【问题标题】:Why am I receiving a ValueError?为什么我会收到 ValueError?
【发布时间】: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


【解决方案1】:

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

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

【讨论】:

  • 我刚刚通过命令行删除了所有的空格。但它仍然产生同样的错误。还有其他原因吗?
  • 不,我认为没有其他原因。尝试在拆分之前添加print(line)。这将向您显示有效的行,更重要的是,它会显示错误发生之前的最后一行
  • 所以我在打印行时发现有些行有标签但没有句子(句子的空格)。许多这样的行。
【解决方案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
 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 准备输入文件。

【讨论】:

    【解决方案3】:

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

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

    所以这一定意味着我们在您提供的内容中缺少某些内容。我认为,如果以上内容不能满足您的需求,则需要更多信息

    【讨论】:

    • 我再次遇到同样的错误。 :( 我不知道如何将内容放在这里,因为它是一个巨大的文件。也许在讨论聊天中?
    • 我想你误会了。错误是在执行split 之后尝试分配labelsentence 时,而不是在追加时。
    • 我知道这是错误,更改是针对我运行的测试。我的观点是,有了可用的数据,就没有错误。为什么不尝试在抛出错误的行周围为我们添加一个 try 块,捕获 ValueError 并打印“行”,然后您可以看到该行并将其添加到此处。
    猜你喜欢
    • 2021-04-20
    • 1970-01-01
    • 2020-04-08
    • 2018-02-04
    • 1970-01-01
    • 2023-04-05
    • 2016-08-21
    • 2014-07-04
    • 2020-06-06
    相关资源
    最近更新 更多