2017-01-14 8 views
-1

私はワードカウントプログラムで作業しています。このコードの値エラー:アンパックするには1つ以上の値が必要です

#!/usr/bin/env python 
import sys 

# maps words to their counts 
word2count = {} 

# input comes from STDIN 
for line in sys.stdin: 

    line = line.strip() 


    word, count = line.split('\t', 1) 

    try: 
     count = int(count) 
    except ValueError: 
     continue 

    try: 
     word2count[word] = word2count[word]+count 
    except: 
     word2count[word] = count 

for word in word2count.keys(): 
    print '%s\t%s'% (word, word2count[word]) 

エラー:

word, count = line.split('\t', 1) 
ValueError : need more than 1 value to unpack 
+2

「行」にタブがないことを意味します。 – jonrsharpe

+0

私もタブでチェックしましたが、同じエラー – Madhu

+0

どういう意味ですか?もともと何を試していたのですか? –

答えて

0

tryword, count = line.split('\t', 1)を移動する - exceptは動作するはずです:これはで番号を持っていないすべての行をスキップするでしょう

for line in sys.stdin: 
    line = line.strip() 
    try: 
     word, count = line.split('\t', 1) 
     count = int(count) 
    except ValueError: 
     continue 

残りの行からタブで区切られた行の先頭。

+0

はい、動作します。ありがとうございます – Madhu

+0

それは助けて偉大な。あなたの問題が解決されたら、回答を受け入れることができます(http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work)。 –

関連する問題