2013-10-24 156 views
-2

我正在運行此代碼。IndexError:'列表索引超出範圍'

import tweetstream 
import csv 

twitterUsername = "USERNAME" 
twitterPassword = "PASSWORD" 

twitterWordFilter = [] #Defined the list 
wordListCsv = csv.reader(open('wordstofilter.csv', 'rb')) 
for row in wordListCsv: 
    #Add the 0th column of the current row to the list 
    twitterWordFilter.append(row[0]) 

print "Filtering the following words: ",', '.join(twitterWordFilter) 

try: 
    with tweetstream.FilterStream(twitterUsername, twitterPassword,track=twitterWordFilter) as stream: 
     for tweet in stream: 
      try: 
       print stream.count,"(",stream.rate,"tweets/sec). ",tweet['user']['screen_name'],':', tweet['text'].encode('utf-8') 
       #print tweet #Use for raw output 
      except: 
       print "ERROR: Presumably missing field" 

except tweetstream.ConnectionError, e: 
    print "Disconnected from twitter. Reason:", e.reason 

當我調試它,它提供了以下錯誤的行twitterWordFilter.append(row[0])

Error : IndexError: 'list index out of range' 

我在做什麼錯?

+1

這意味着'行'是空的。 –

+0

請提供更多詳情 –

回答

0

< < <原始代碼

wordListCsv = csv.reader(open('wordstofilter.csv', 'rb')) 
for row in wordListCsv: 
     #Add the 0th column of the current row to the list 
    twitterWordFilter.append(row[0]) 

原始代碼>>>

這裏,

CSV文件是空的,所以它返回一個[](空單)wordListCsv

twitterWordFilter.append(row[0]) 

個,並且您試圖訪問其不再是現有的元素...

0
wordListCsv = csv.reader(open('wordstofilter.csv', 'rt')) 
for row in wordListCsv: 

CSV文件文本,需要開這樣。

相關問題