Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何修复ValueError:太多的值无法解包;用Python?_Python_Python 3.x - Fatal编程技术网

如何修复ValueError:太多的值无法解包;用Python?

如何修复ValueError:太多的值无法解包;用Python?,python,python-3.x,Python,Python 3.x,我试图用文本文件(“out3.txt”)的内容填充字典 我的文本文件的格式如下: vs,14100 mln,11491 the,7973 cts,7757 answer[vs]=14100 answer[mln]=11491 ……等等 我希望我的字典答案的格式如下: vs,14100 mln,11491 the,7973 cts,7757 answer[vs]=14100 answer[mln]=11491 ……等等 我的代码是: import os import col

我试图用文本文件(“out3.txt”)的内容填充字典

我的文本文件的格式如下:

vs,14100

mln,11491

the,7973

cts,7757
answer[vs]=14100

answer[mln]=11491
……等等

我希望我的字典
答案
的格式如下:

vs,14100

mln,11491

the,7973

cts,7757
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:要解压缩的值太多


如何修复此问题?

您的输入文件中有空的
s,我怀疑您没有与我们共享的
行中有一行
s中有太多逗号(因此“太多值无法解包”)

您可以防止这种情况,如下所示:

import collections

answer = collections.defaultdict(list)
with open('out3.txt', 'r+') as istream:
    for line in istream:
        line = line.strip()
        try:
            k, v = line.split(',', 1)
            answer[k.strip()].append(v.strip())
        except ValueError:
            print('Ignoring: malformed line: "{}"'.format(line))

print(answer)

注意:通过将
1
传入,第一个逗号后的所有内容都将分配给
v
;如果这不是您想要的行为,并且您希望这些行被拒绝,您可以删除此参数。

您的解决方案不会给出您想要的输出。您将有(假设它起作用),
答案['vs']=[14100]
,下面是您想要的:

import csv

with open('out3.txt') as f:
  reader = csv.reader(f, delimiter=',')
  answer = {line[0].strip():line[1].strip() for line in reader if line}

这里不需要
集合
。简单的旧格言就足够了:

answer = {}
with open('out3.txt', 'r+') as f:
    for line in f:
        lst = line.split(',')
        if len(lst) == 2:
            k = lst[0].strip()
            v = lst[1].strip()
            answer[k] = v

print(answer['mln'])
print(answer.get('xxx', 'not available'))
请注意
answer.get()
answer[]
类似,但您可以提供默认值


您不应该在循环中使用
.readlines()
。即使是空行也包含换行符。这样,测试
if line:
不会检测到空行。或者您必须先剥离(或
rstrip
)它,或者您可以将行拆分为列表并测试元素的数量

我怀疑输入文件中的一行有多个逗号。请尝试
grep',.*,'out3.txt
。可以删除第一个
answer={}
。打开的文件应始终关闭。感谢@pepr,当我移除剩余的积垢时,我错过了这一点。+1因为使用了
csv
(除非输入文件非常具体、简单)。