繁体   English   中英

为什么会收到ValueError?

[英]Why am I receiving a ValueError?

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

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

文件2

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

但是出现相同的错误。

它们不能完全相同。 我的猜测是您的第二个文件中某处有一个空/仅空白行,最有可能在结尾处。

错误告诉您,当执行拆分时,没有空格可拆分,因此仅返回一个值,而不是labelsentence的值。

根据您的编辑,我怀疑您的文本文件中可能仍然有“空”行。 好吧,我可能最好说:除了空白以外,什么都没有填充的行。

我扩展了您的示例文件:

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 -magic准备输入文件。

根据以上提供的内容(进行了调整)。 这似乎给出了预期的结果。

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']}

因此,这必须表示我们所提供的内容中缺少某些内容。 我认为,如果以上内容无法满足您的需求,则需要更多信息

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM