【发布时间】:2016-03-08 13:48:28
【问题描述】:
我有两个看起来完全相同的文件: 文件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
文件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
但是出现了同样的错误。
【问题讨论】:
-
第二个文件的末尾是否有空行/仅包含空格的行?
-
一般来说:
import pdb; pdb.pm()会将您置于异常中,您可以转储出line并查看它到底发生了什么故障。 -
我尝试添加一个 if no line: continue ,但它再次给出了同样的错误。可能有一个空行
-
你确定吗?我可以通过添加一个空行来重现您的错误。然而,
if line.strip():很容易修复。灵感来自这里:stackoverflow.com/a/7896585/1063730 :) -
@yoshi 如果我尝试忽略空行,我仍然会得到相同的值错误。还有什么办法可以解决吗?
标签: python