如何从文件制作字典?

时间:2021-02-24 11:47:16

标签: python list dictionary

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

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:\data\GG\Desktop\Extra\UK_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']}

2 个答案:

答案 0 :(得分:2)

每行有 4 列


from collections import defaultdict 

path = (r'c:\data\GG\Desktop\Extra\UK_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

答案 1 :(得分:1)

my_dict = {}
path = r'c:\data\GG\Desktop\Extra\UK_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:\data\GG\Desktop\Extra\UK_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']}