IndexError: List index out of range - Python CSV

喜欢而已 提交于 2019-12-17 22:00:19

问题


I am pulling 10 tweets from Twitter using tweepy and storing it in a CSV for later displaying it on the front-end webpage. My code refreshes every 60 minutes, and at certain times I get the "IndexError".

Following is the exact error:

return ks[5]

IndexError: List index out of range

Following is the function to retrieve the particular tweet from CSV:

def tweet6(self):
    with codecs.open('HELLOTWITTER.csv', 'r', encoding='utf-8', errors='ignore') as f:
        reader = csv.reader(f)
        d = {}
        for i, row in enumerate(reader):
            d[row[0]]=row[1:]
            if (i>=10):
                break    
    ks=list(d)
    return (ks[5])

This error occurs only at times but I am unable to figure out why it happens although the CSV has all the 10 tweets written into it every time the entire code is refreshed. Also, if I run the code once more, the error disappears and the webpage loads without any issues with the tweets, surprisingly!

What am I missing? Any help is much appreciated! Thanks!


回答1:


As Ken White pointed out in the comments above. The error is caused by you trying to access an index that is outside the bounds of the list.

What is going on is that there is a blank row in your CSV file that python cannot process because you are calling index 0 even though it does not exist therefore python throws an exception.

In order to fix this error what you need to do is check if there are enough elements in the list to run your code. By using

if(len(row) < 1):
   continue

Another place that could be causing problems is where you are taking the list d and putting it inside another list ks. Then you try to return the 5th object in the new list. However, there is no object because you now have a list that looks like this

ks={{tweet,tweetyouwant,tweet},{list,two,if,present}}

When you were expecting the list to look like this

ks={tweet,tweetyouwant,tweet}

In order to fix this simply get rid of ks=list(d) and call d wherever you would call ks


Your whole snippet should like this.

def tweet6(self):
    with codecs.open('HELLOTWITTER.csv', 'r', encoding='utf-8', errors='ignore') as f:
        reader = csv.reader(f)
        d = {}
        for i, row in enumerate(reader):

            #Verify row is within range
            if(len(row) < 1):
                continue

            #Get the rows values
            d[row[0]]=row[1:]

            #If past row 10 then break
            if (i>=10):
                break  

    #ks=list(d) #Not needed D is already a list
    return (d[5]) #return the row of the 6th tweet


来源:https://stackoverflow.com/questions/47585688/indexerror-list-index-out-of-range-python-csv

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!