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!
prints to check if the variables contain the expected results (or using a debugger instead) is generally helpful