Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/342.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/angular/31.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
Python ValueError:太多的值可能无法解包_Python - Fatal编程技术网

Python ValueError:太多的值可能无法解包

Python ValueError:太多的值可能无法解包,python,Python,有人能提供一些意见吗 for line in oscars_file: if '-' in line: years,oscars=line.strip().split('-') 我在终端中得到了这个错误: ValueError: too many values to unpack (expected 2) oscars文件中的一个示例是: 1975 - "One Flew over the Cuckoo's Nest" 1973 - "The Sting" 某些文

有人能提供一些意见吗

for line in oscars_file:
    if '-' in line:
        years,oscars=line.strip().split('-')
我在终端中得到了这个错误:

ValueError: too many values to unpack (expected 2)
oscars文件中的一个示例是:

1975 - "One Flew over the Cuckoo's Nest"

1973 - "The Sting"

某些文本可能包含1个以上的
'-'
。为此,您应该:

for line in oscars_file:
    if '-' in line:
        years,oscars=line.strip().split('-',1)
拆分('-',1)
只进行一次拆分,这是您想要的第一次拆分

例子
某些文本可能包含1个以上的
'-'
。为此,您应该:

for line in oscars_file:
    if '-' in line:
        years,oscars=line.strip().split('-',1)
拆分('-',1)
只进行一次拆分,这是您想要的第一次拆分

例子
代码是正确的(至少在我的电脑中),检查你的oscars文件,它看起来有问题。代码是正确的(至少在我的电脑中),检查你的oscars文件,它看起来有问题。比我的答案要好得多。事实上,我将删除我的答案。@user3046660,如果答案是您想要的,您应该单击答案左侧的勾号将其选中。如果你选择了一个比我的答案更好的答案作为解决方案,人们会更愿意回答你。事实上,我将删除我的答案。@user3046660,如果答案是您想要的,您应该单击答案左侧的勾号将其选中。如果您选择答案作为解决方案,人们更可能回答您()