Sara
Sara

Reputation: 11

How to extract tweet and store it in CSV file?

I have write this code to save the id and tweet I extract from specific user but the problem is it save only the first tweet with index 50 I try the counter but nothing happens.

a=50
for info in tweets[:a]:
   with open(userID+'.csv', 'w', newline='') as file:
     writer = csv.writer(file)
     writer.writerow(["id", "tweet"])
     writer.writerow([info.id,info.full_text+ "\n"])
     a-=1

Upvotes: 0

Views: 83

Answers (1)

Nishant Agarwal
Nishant Agarwal

Reputation: 455

Use append mode instead of write.

with open(userID+'.csv', 'a', newline='') as file:

'w' : write mode actually replaces the existing content. 'a' : append mode adds data to the existing file. Read here : https://www.guru99.com/reading-and-writing-files-in-python.html

Upvotes: 1

Related Questions