IndexError: List index out of range - Python CSV

断了今生、忘了曾经 提交于 2019-11-28 14:13:39

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