2

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!

5
  • Please format the code properly. Commented Dec 1, 2017 at 2:18
  • Placing a few prints to check if the variables contain the expected results (or using a debugger instead) is generally helpful Commented Dec 1, 2017 at 2:18
  • 3
    Clearly at some point you're in error about the CVS has all of the 10 tweets written into it every time. Otherwise, you're expecting us to believe that Python is lying about it and just pretending. list index out of range means that the list index is out of range, and the only reason you get that error is if the list index is out of range. Commented Dec 1, 2017 at 2:27
  • @KenWhite : I understand that it's an issue with a blank row or insufficient data. But is there any way I can handle this error? Commented Dec 4, 2017 at 2:49
  • 1
    I think the answer you've accepted deals with that, doesn't it? Commented Dec 4, 2017 at 3:03

1 Answer 1

3

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
Sign up to request clarification or add additional context in comments.

4 Comments

The issue could also be caused by ks[5] however I do not think that would be as likely as row not having enough data in it
Hey I still continue to get the IndexError. Anything else I could do? :/
Did you check ks[5]? That seems to be the only other place I can think of that would cause problems. My next tip is to add print statements between every line to see where it is breaking. So that way it is easier to know where the bug is
Looking at your OP it is definitely ks5 that is returning the error. Try just returning D. You already have a list D that you are then putting in a second list with ks=list(d) sorry for the lack of formatting I am on mobile

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.