2016-04-01 29 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

相關問題