【发布时间】:2019-11-16 19:29:19
【问题描述】:
我的文件是这样的:
House Plant, 2, 5, 6
House Plant1, 4, 5, 7
... and so on
我希望这两个词作为键,数字作为整数值,并将所有行放入字典中。
{'House Plant':[2,5,6],'House Plant1':[4,5,7], etc}
这并不是真的这样工作:
dictionary = {}
with open("persons.dat","r") as file:
for line in file:
items = line.split()
key, values = items[1], items[2:]
dictionary.setdefault(key,[]).extend(values)
print(items)
【问题讨论】:
标签: python file dictionary