Skip to main content
 首页 » 编程设计

python之如何使用 Python 分隔值

2025年05月04日21fff_TT

<分区>

如何使用 Python 分隔值?我试过 split 和 linespace 但它们没有按照我期望的方式拆分数据

我的 .txt 文件包含以下内容:

{0: 'tench, Tinca tinca',  
1: 'goldfish, Carassius auratus',  
2: 'great white shark, white shark, man-eater, man-eating carcharias', 3: 'tiger shark, Galeocerdo cuvieri',  
4: 'hammerhead, hammerhead shark',  
5: 'electric ray, crampfish, numbfish, torpedo',}  

我正在寻找输出 key = [0,1,2,3,...] Values = ['tench, Tinca tinca','goldfish, Carassius auratus ',...] 或者我能以某种方式将它转换成字典吗?我尝试使用参数 (',') 进行拆分,它拆分了 'tench,但我想要 'tench, Tinca tinca' 作为输出。

这是我卡住的代码

f = open('imagenet1000_clsid_to_human.txt', 'r')  
x = f.read().lower().strip().split("',")  
y = []  
for i in x: (y.append(i))  
    print(y)