Python ValueError: 需要 1 个以上的值才能解包,拆分一行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20270871/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ValueError: need more than 1 value to unpack, split a line
提问by user3046660
I have a file with questions and answers on the same line, I want to seperate them and append them to their own empty list but keep getting this error:builtins.ValueError: need more than 1 value to unpack
我在同一行有一个包含问题和答案的文件,我想将它们分开并将它们附加到它们自己的空列表中,但不断收到此错误:builtins.ValueError: need more than 1 value to unpack
questions_list = []
answers_list = []
questions_file=open('qanda.txt','r')
for line in questions_file:
line=line.strip()
questions,answers =line.split(':')
questions_list.append(questions)
answers_list.append(answers)
采纳答案by Games Brainiac
This is probably because when you're doing the splitting, there is no :, so the function just returns one argument, and not 2. This is probably caused by the last line, meaning that you're last line has nothing but empty spaces. Like so:
这可能是因为当您进行拆分时,没有:,因此该函数只返回一个参数,而不是 2。这可能是由最后一行引起的,这意味着您的最后一行只有空格。像这样:
>>> a = ' '
>>> a = a.strip()
>>> a
''
>>> a.split(':')
['']
As you can see, the list returned from .splitis just a single empty string. So, just to show you a demo, this is a sample file:
如您所见,从返回的列表.split只是一个空字符串。所以,只是为了向您展示一个演示,这是一个示例文件:


We try to use the following script (val.txtis the name of the above file):
我们尝试使用以下脚本(val.txt是上述文件的名称):
with open('val.txt', 'r') as v:
for line in v:
a, b = line.split(':')
print a, b
And this gives us:
这给了我们:
Traceback (most recent call last):
a b
c d
File "C:/Nafiul Stuff/Python/testingZone/28_11_13/val.py", line 3, in <module>
a, b = line.split(':')
e f
ValueError: need more than 1 value to unpack
When trying to look at this through a debugger, the variable linebecomes \n, and you can't split that.
当尝试通过调试器查看此变量line时\n,变量变为,并且您无法拆分它。
However, a simple logical ammendment, would correct this problem:
然而,一个简单的逻辑修正,将纠正这个问题:
with open('val.txt', 'r') as v:
for line in v:
if ':' in line:
a, b = line.strip().split(':')
print a, b
回答by Simeon Visser
line.split(':')apparently returns a list with one element, not two.
line.split(':')显然返回一个包含一个元素而不是两个元素的列表。
Hence that's why it can't unpack the result into questionsand answers. Example:
因此,这就是为什么它不能将结果解包到questionsand 中的原因answers。例子:
>>> line = 'this-line-does-not-contain-a-colon'
>>> question, answers = line.split(':')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: need more than 1 value to unpack
回答by Kos
Try:
尝试:
question, answers = line.split(':', maxsplit=1)
question, __, answers = line.partition(':')
Also in Python 3 you can do something else:
同样在 Python 3 中,您还可以做其他事情:
question, *many_answers = line.split(':')
which looks like:
看起来像:
temp = line.split(':')
question = temp[0]
many_answers = tuple(temp[1:])

