得到IndexError:字符串索引超出范围

时间:2017-12-14 04:26:29

标签: sentiment-analysis

我正在尝试此代码,我收到以下错误。 有人,请帮帮我!!

#Read the tweets one by one and process it
import csv
inpTweets = csv.reader(str(open('tweets.csv', 'rb')), delimiter=',', 
quotechar='|')
tweets = []
for row in inpTweets:
    sentiment = row[0]
    tweet = row[1]
    processedTweet = processTweet(tweet)
    featureVector = getFeatureVector(processedTweet, stopWords)
    tweets.append((featureVector, sentiment));
#end loop




IndexError                                Traceback (most recent call last)
 <ipython-input-15-6eb83cd8111f> in <module>()
        7 for row in inpTweets:
        8     sentiment = row[0]
  ----> 9     tweet = row[1]
       10     processedTweet = processTweet(tweet)
       11     featureVector = getFeatureVector(processedTweet, stopWords)

  IndexError: string index out of range

请帮忙......

1 个答案:

答案 0 :(得分:1)

我认为您已尝试将打开的文件转换为字符串,而不是逐行读取并使用分隔符。 https://docs.python.org/3.4/library/csv.html

with open('file.csv', 'rb') as csvfile:
data = csv.reader(csvfile, delimiter=',', quotechar='|')
    for row in data:
        print(row, end=',')