我正在尝试使用我的文本文件(“out3.txt”)中的内容填充字典。
我的文本文件的格式如下:
vs,14100
mln,11491
the,7973
cts,7757
我希望我的字典answer
的格式为:
answer[vs]=14100
answer[mln]=11491
我的代码是:
import os
import collections
import re
from collections import defaultdict
answer = {}
answer=collections.defaultdict(list)
with open('out3.txt', 'r+') as istream:
for line in istream.readlines():
k,v = line.strip().split(',')
answer[k.strip()].append( v.strip())
但是,我收到以下错误信息:
ValueError: too many values to unpack
我该如何修复这个问题?
grep ',.*,' out3.txt
。 - Marcelo Cantos