简体   繁体   中英

why am I getting this error? twitter_list_dict.append(line[0]) IndexError: list index out of range

Traceback (most recent call last):
  File "D:\myscripts\NewTermSentimentInference.py", line 88, in <module>
    main()
  File "D:\myscripts\NewTermSentimentInference.py", line 34, in main
    tweets = tweet_dict(twitterData)
  File "D:\myscripts\NewTermSentimentInference.py", line 15, in tweet_dict
    twitter_list_dict.append(line[0])
IndexError: list index out of range

Code:

twitterData = sys.argv[0] # csv file

def tweet_dict(twitterData):  
    ''' (file) -> list of dictionaries
    This method should take your csv file
    file and create a list of dictionaries.
    '''
    twitter_list_dict = []
    twitterfile = open(twitterData)
    twitterreader = csv.reader(twitterfile)
    for line in twitterreader:
        **twitter_list_dict.append(line[1])**
    return twitter_list_dict


def sentiment_dict(sentimentData):
    ''' (file) -> dictionary
    This method should take your sentiment file
    and create a dictionary in the form {word: value}
    '''
    afinnfile = open(sentimentData)
    scores = {} # initialize an empty dictionary
    for line in afinnfile:
        term, score  = line.split("\t")  # The file is tab-delimited. "\t" means "tab character"
        scores[term] = float(score)  # Convert the score to an integer.   
    return scores # Print every (term, score) pair in the dictionary


def main():

    tweets = tweet_dict(twitterData)
    sentiment = sentiment_dict("AFINN-111.txt")
    accum_term = dict()

    """Calculating sentiment scores for the whole tweet with unknown terms set to score of zero
    See -> DeriveTweetSentimentEasy
    """

    for index in range(len(tweets)):

        tweet_word = tweets[index].split()
        sent_score = 0 # sentiment of the sentence
        term_count = {}
        term_list = []

Trying to do sentiment analysis but facing Index error in the line in this portion of code in the method which tries to create dictionaries from a csv file which has tweets accessed from twitter, can someone please help me with it?

Check the input CSV for empty lines.

The 'list index out of range' error will get thrown in twitter_list_dict.append(line[0]) if line is an empty list, and hence has no first element to reference. The most likely culprit: one or more of the lines in the CSV is empty, which will lead csv.reader to return an empty list for line .

If empty lines in the CSV are expected, you can skip them by adding a check to ensure the line isn't empty:

for line in twitterreader:
  if line: # Check if line is non-empty
    twitter_list_dict.append(line[0])
return twitter_list_dict

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM