-2

我正在运行这段代码。

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' 

我究竟做错了什么?

4

2 回答 2

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])

并且您正在尝试访问不再存在的元素...

于 2013-10-24T18:22:19.830 回答
0
wordListCsv = csv.reader(open('wordstofilter.csv', 'rt'))
for row in wordListCsv:

CSV 文件是文本,需要按原样打开。

于 2013-10-24T18:25:01.223 回答