2016-04-01 27 views
1

我想打开一些文本文件读取它们,并从这些文件中获取所有字符从a-z并做一些更多的处理与这些字符。ValueError:太多的值解压缩,读取文本文件时出错

,但我得到的错误ValueError: too many values to unpack

这里是我的脚本的启动及其对这个代码块,我得到的错误

for line in sys.stdin: 

    if ":" in line: 

     filename, line = line.strip().split(':') # this line gives error 

它是与分裂的注释行它似乎。我之所以做这种方式是因为我也想提取文件名,我看别的地方之前,当标准输入读取文件格式的

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

我也有试过这种在一个单一的文本文件和它的工作,但现在我尝试整批我得到这个

我把它从控制台这样

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

错误是在第一个脚本脚本

大局观从第1文本文件

#!/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

@UlrichEckhardt第二行,它实际上是第三行,我评论它更清楚,我也把输入放在那里的一个文本文件,至于变量的状态发生在任何事情打印出来之前,我做了之后上面的代码块,所以它似乎停止脚本 –

+0

你应该自己减少你的问题。它是否读取了一条失败的行?它是处理与某些内容的线?专注于这个问题,提取一个最小的例子来展示未被发现的行为并在此发布。网站规则明确要求这样做,因为它避免了如果人们试图仔细减少和分析代码就会自动解决的问题。 –

回答

6

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

尝试指定最大划分金额:

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

或者,使用'.partition',以便始终得到三个结果,并且可以测试它是否匹配而不是引发异常(个人首选项)。例如'filename,sep,line = line.strip()。partition(':')','if sep:... skip line ...' – ShadowRanger

相关问题