简体   繁体   English

为什么我收到此错误? twitter_list_dict.append(line [0])IndexError:列表索引超出范围

[英]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? 尝试进行情绪分析,但是在试图从csv文件中创建字典的方法中的代码部分的中面对索引错误,该文件具有从Twitter访问的推文,有人可以帮助我吗?

Check the input CSV for empty lines. 检查输入CSV是否为空行。

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. 如果line是一个空列表,那么'list index out of range'错误将在twitter_list_dict.append(line[0])抛出,因此没有第一个要引用的元素。 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 . 最有可能是罪魁祸首:一个或多个在CSV行是空的,这将导致csv.reader返回一个空列表line

If empty lines in the CSV are expected, you can skip them by adding a check to ensure the line isn't empty: 如果预期CSV中有空行,您可以通过添加检查来跳过它们以确保该行不为空:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我收到 IndexError: list index out of range 这个函数? - Why am I getting IndexError: list index out of range for this function? 为什么我收到 IndexError: list index out of range - Why am I getting IndexError: list index out of range 为什么在此代码中出现IndexError:list index超出范围? - Why am I getting IndexError: list index out of range in this code? 为什么我得到 IndexError: list index out of range 错误,即使我有一个正确的列表 - Why am I getting IndexError: list index out of range error even though I have a proper list 为什么我会收到此错误? IndexError:列表索引超出范围 - Why am I receiving this error? IndexError: list index out of range 初学者:为什么我会收到此错误:IndexError: list assignment index out of range - Beginner: Why am I getting this error: IndexError: list assignment index out of range Python:不知道为什么会出现“ IndexError:列表分配索引超出范围”错误 - Python: not sure why I am getting “IndexError: list assignment index out of range” error 为什么我在 Python3 中收到此错误:IndexError:列表索引超出范围 - Why am I getting this error in Python3: IndexError: list index out of range 我收到IndexError:列表索引超出范围 - I am getting IndexError: list index out of range 当我的代码似乎在范围内时,为什么会出现 IndexError: list index out of range? - Why am I getting an IndexError: list index out of range when my code appears to be within the range?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM