我想填充我的文本文件的內容的字典嗎?(‘out3.txt’)如何解決ValueError異常:太多值解壓」在Python
我的文本文件,是的。形式:
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'。 –