如何从文件中创建字典?



我有以下文件(制表符分隔):

ID  Name    Abbr    Disctrict
*data*
1   Newcastle   NC  AA,BB,CC
2   Manchester  MCR AA,DD,FF
3   Liverpool   LV  FF,GG,HH

我试图将index[1]作为键和index[3]作为字典内的值。它似乎还不起作用

my_dict = {}
path = (r'c:dataGGDesktopExtraUK_test_cities.txt')
with open(path, 'r') as f:
for line in f:
(key, val) = line.split()
my_dict[int(key)] = val

这是我的输出:

File "c:/data/GG/Desktop/Extra/test_1.py", line 17, in <module>
(key, val) = line.split()
ValueError: too many values to unpack (expected 2)

这是我期望的输出:

my_dict = {'Newcastle': ['AA,BB,CC'],
'Manchester': ['AA,DD,FF'],
'Liverpool': ['FF,GG,HH']}

每行4列


from collections import defaultdict 
path = (r'c:dataGGDesktopExtraUK_test_cities.txt')
my_dict=defaultdict()
with open(path, 'r') as f:
content = f.readlines()
for line in content[2:]: #First two line does not contain data
_,key,_,val = line.split()
my_dict[key] = val
my_dict = {}
path = r'c:dataGGDesktopExtraUK_test_cities.txt'
with open(path, 'r') as f:
for _ in range(2): # skip first 2 lines
next(f)
for line in f:
idx, name, abbr, district = line.split('t')
my_dict[name] = district.strip().split(',')
print(my_dict)

也可以使用csv模块或pandas。

import pandas as pd
path = r'c:dataGGDesktopExtraUK_test_cities.txt'
df = pd.read_csv(path, sep='t', skiprows=2, index_col='Name',
usecols=[1, 3], names=['Name', 'District'])
df['District'] = df['District'].str.split(',')
my_dict = df.T.to_dict('index')['District']
print(my_dict)
print(my_dict)

两个片段都会生成

{'Newcastle': ['AA', 'BB', 'CC'], 'Manchester': ['AA', 'DD', 'FF'], 'Liverpool': ['FF', 'GG', 'HH']}

相关内容

  • 没有找到相关文章

最新更新