为什么我收到ValueError?

时间:2016-03-08 13:48:28

标签: python

我有两个看起来完全相同的文件: 文件1

1 in seattle today the secretary of education richard riley delivered his address 
1 one of the things he focused on as the president had done
1 abc's michele norris has been investigating this
2 we're going to take a closer look tonight at the difficulty of getting meaningful

file2的

1 in seattl today the secretari of educ richard riley deliv hi address
1 one of the thing he focus on a the presid had done
1 abc michel norri ha been investig thi 
2 we'r go to take a closer look tonight at the difficulti of get meaning

当我运行此代码时:

result=defaultdict(list)
with open("onthis.txt","r") as filer:
    for line in filer:
        label, sentence= line.strip().split(' ', 1)
        result[label].append(sentence)

它适用于file1,但是为file2提供了一个值错误:

label, sentence= line.strip().split(' ', 1)
ValueError: need more than 1 value to unpack

当他们两种格式相同时,我似乎无法理解。 所以,我刚用这个终端命令删除了空行:

sed '/^$/d' onthis.txt > trial

但是出现了同样的错误。

3 个答案:

答案 0 :(得分:1)

它们不能完全相同。我的猜测是你的第二个文件中有一个空的/只有空格的行,很可能就在最后。

错误告诉您,当它执行拆分时,没有要拆分的空格,因此只返回一个值,而不是labelsentence的值。< / p>

答案 1 :(得分:1)

根据您的编辑,我怀疑您的文本文件中可能仍有“空”行。好吧,我可能更应该说:除了空格之外什么都没有。

我扩展了您的示例文件:

1 in seattl today the secretari of educ richard riley deliv hi address
1 one of the thing he focus on a the presid had done
1 abc michel norri ha been investig thi 
2 we'r go to take a closer look tonight at the difficulti of get meaning
 3   foo

4 bar


5 qun

可能不太清楚但是3 foo4 bar之间的线由几个空格填充,而4 bar 5 qun之间的线是“只是”新线(\n)。

注意sed '/^$/d'

的输出
1 in seattl today the secretari of educ richard riley deliv hi address
1 one of the thing he focus on a the presid had done
1 abc michel norri ha been investig thi 
2 we'r go to take a closer look tonight at the difficulti of get meaning
 3   foo

4 bar
5 qun

空线被真正删除 - 毫无疑问。但伪空白线仍然存在。运行python脚本将在到达此行时抛出错误:

2 we'r go to take a closer look tonight at the difficulti of get meaning

 3   foo    

Traceback (most recent call last):
  File "python.py", line 9, in <module>
    label, sentence= line.strip().split(' ', 1)
ValueError: need more than 1 value to unpack

所以我的建议是将你的脚本扩展一行,使其在输入文件中跳过空行。

for line in filer:
    if not line.strip(): continue

这样做会产生积极的副作用,您无需使用某些sed魔法准备输入文件。

答案 2 :(得分:-1)

基于您提供的上述内容(带有调整)。这似乎给出了预期的结果。

result = {}

with open("test.txt", "r") as filer:
    for line in filer:
        label, sentence = line.strip().split(' ', 1)
        try:
            result[label].append(sentence)
        except KeyError:
            result[label] = [sentence]

输出:

{'2': ["we'r go to take a closer look tonight at the difficulti of get meaning"], '1': ['in seattl today the secretari of educ richard riley deliv hi address', 'one of the thing he focus on a the presid had done', 'abc michel norri ha been investig thi']}

所以这必然意味着我们提供的东西中缺少某些东西。我认为,如果上述内容无法满足您的需求,则需要更多信息