ValueError:解压缩的值太多,读取文本文件时出错

时间:2016-04-01 15:00:02

标签: python

我正在尝试打开一些读取它们的文本文件,并从这些文件中获取a-z中的所有字符,并使用这些字符进行更多处理。

但我收到错误ValueError: too many values to unpack

这是我的脚本的开始,它在此代码块的注释行上,我收到错误

for line in sys.stdin:

    if ":" in line:

        filename, line = line.strip().split(':') # this line gives error
它似乎与拆分有关。我这样做的原因是因为我想要提取文件名,我在此之前读取其他地方,当stdin读取文件时,其格式为

filename.txt: Start of the first line inside the text file

我也在一个文本文件上尝试了这个并且它工作但现在我尝试完整批次我得到这个

我从控制台这样称呼它

grep -r '' susp-text | ./mapper.py | sort | ./suspicious_reducer.py

错误发生在第一个脚本mapper.py

更大的剧本图片

#!/usr/bin/env python

import sys
import re

# regular expressions

pattern = re.compile("[a-zA-Z]*",
                 re.MULTILINE | re.DOTALL | re.IGNORECASE)

a_to_f_pattern = re.compile("[a-fA-F]", re.IGNORECASE)
g_to_l_pattern = re.compile("[g-lG-L]", re.IGNORECASE)
m_to_r_pattern = re.compile("[m-rM-R]", re.IGNORECASE)
s_to_z_pattern = re.compile("[s-zS-Z]", re.IGNORECASE)

# Read pairs as lines of input from STDIN
for line in sys.stdin:
    print line
    if ":" in line:

        filename, line = line.strip().split(':')
        filename = filename.replace("source_text/", "")
        filename = filename.replace("suspicious_text/", "")

        # loop through every word that matches the pattern
        for word in pattern.findall(line):
            while i < len(word):

从第一个读取的文本文件中提取

 Even without the
nets, caught she will be, from sheer fatigue, (15) owing to the depth of the snow, which balls
itself under her shaggy feet and clings to her, a sheer dead weight.

 (11) Al. "to envelop the victims in the nets."

 (12) Lit. "whatever the creature is in contact with inside."

 (13) Cf. Aesch. "Prom." 87, {Poto tropo tesd' ekkulisthesei tukhes}.

 (14) Or, "if the creature is not first suffocated in the snow itself."

 (15) See Pollux, v. 50. "She must presently be tired out in the heavy
    snow, which balls itself like a fatal clog clinging to the under
    part of her hairy feet."

1 个答案:

答案 0 :(得分:6)

听起来你可能有一个以上的线路#34;:#34;在里面。在这种情况下,split将返回一个包含两个以上项目的列表,这些项目太多而无法放入两个变量中。

尝试指定最大分割量:

filename, line = line.strip().split(':', 1)