Python 列表索引超出范围错误,TextBlob为csv

Python 列表索引超出范围错误,TextBlob为csv,python,csv,nlp,textblob,Python,Csv,Nlp,Textblob,我有一个巨大的csv,上面有来自我博客的数千条评论,我想使用textblob和nltk进行情绪分析 我使用的是来自的python脚本,但为Python3进行了修改 ''' uses TextBlob to obtain sentiment for unique tweets ''' from importlib import reload import csv from textblob import TextBlob import sys # to force utf-8 encoding

我有一个巨大的csv,上面有来自我博客的数千条评论,我想使用textblob和nltk进行情绪分析

我使用的是来自的python脚本,但为Python3进行了修改

'''
uses TextBlob to obtain sentiment for unique tweets
'''

from importlib import reload
import csv
from textblob import TextBlob
import sys

# to force utf-8 encoding on entire program
#sys.setdefaultencoding('utf8')

alltweets = csv.reader(open("/path/to/file.csv", 'r', encoding="utf8", newline=''))
sntTweets = csv.writer(open("/path/to/outputfile.csv", "w", newline=''))

for row in alltweets:
    blob = TextBlob(row[2])
    print (blob.sentiment.polarity)
    if blob.sentiment.polarity > 0:
        sntTweets.writerow([row[0], row[1], row[2], row[3], blob.sentiment.polarity, "positive"])
    elif blob.sentiment.polarity < 0:
        sntTweets.writerow([row[0], row[1], row[2], row[3], blob.sentiment.polarity, "negative"])
    elif blob.sentment.polarity == 0.0:
        sntTweets.writerow([row[0], row[1], row[2], row[3], blob.sentiment.polarity, "neutral"])
“”
使用TextBlob获取独特推文的情感
'''
从导入lib导入重新加载
导入csv
从textblob导入textblob
导入系统
#在整个程序上强制进行utf-8编码
#sys.setdefaultencoding('utf8')
alltweets=csv.reader(打开(“/path/to/file.csv”,“r”,encoding=“utf8”,newline=”)
sntTweets=csv.writer(打开(“/path/to/outputfile.csv”,“w”,换行符=”)
对于所有tweets中的行:
blob=TextBlob(第[2]行)
打印(斑点、情感、极性)
如果blob.touction.polarity>0:
sntTweets.writerow([row[0],row[1],row[2],row[3],blob.touction.polarity,“positive”])
elif blob.touction.polarity<0:
sntTweets.writerow([row[0],row[1],row[2],row[3],blob.touction.polarity,“negative”])
elif blob.sent.polarity==0.0:
sntTweets.writerow([行[0],行[1],行[2],行[3],blob.touction.polarity,“中立])
然而,当我运行这个时,我不断地

    $ python3 sentiment.py
Traceback (most recent call last):
  File "sentiment.py", line 17, in <module>
    blob = TextBlob(row[2])
IndexError: list index out of range
$python3.py
回溯(最近一次呼叫最后一次):
文件“touction.py”,第17行,在
blob=TextBlob(第[2]行)
索引器:列表索引超出范围
我知道错误的意思,但我不确定我需要做什么来修复


有没有想过我错过了什么?谢谢

玩了一会儿之后,我用熊猫想出了一个更优雅的解决方案

from textblob import TextBlob
import pandas as pd

df = pd.read_csv("pathtoinput.csv", na_values='', 
encoding='utf8',keep_default_na=False, low_memory=False)

columns = ['text']

df = df[columns]

df['tweet'] = df['text'].astype('str')

df['polarity'] = df['tweet'].apply(lambda tweet: 
TextBlob(tweet).sentiment.polarity)

df.loc[df.polarity > 0, 'sentiment'] ='positive'
df.loc[df.polarity == 0, 'sentiment'] ='neutral'
df.loc[df.polarity < 0, 'sentiment'] ='negative'

df.to_csv("pathtooutput.csv", encoding='utf-8', index=False)
从textblob导入textblob
作为pd进口熊猫
df=pd.read\u csv(“pathtoinput.csv”,na\u值=”,
编码='utf8',保持\u默认值\u na=False,低\u内存=False)
列=['text']
df=df[列]
df['tweet']=df['text'].astype('str')
df['polarity']=df['tweet']。应用(lambda tweet:
TextBlob(推特)。情绪。极性)
df.loc[df.polarity>0,“情绪”]=“积极”
df.loc[df.polarity==0,'情绪']='neutral'
df.loc[df.polarity<0,'情绪']='负面'
df.to_csv(“pathtooutput.csv”,encoding='utf-8',index=False)

输入文件中的列数似乎少于3列。请检查是否是这种情况。
如果len(row)<3:继续
?@YoavAbadi My input csv只有一列。我将首先尝试添加两个空columns@YoavAbadi这在一定程度上帮助了我,但随后我在输出中不断遇到奇怪的格式问题。我提供了一个最终有效的答案——使用pandas@BearBrown那不太管用。但我最终找到了一个解决方案,发布在这里。谢谢