Search code examples
pythoniteratorexport-to-csv

Python print to csv in iteration, save rows without line spaces


Hello I will save this output to a csv file after iteration, print is ok, but csv not. Below are the print output

1580933840499281920 2022-10-14 14:49:34+00:00 illiabogdanov en RT @alexstubb: Dear @elonmusk, the war in Ukraine is about life and death, freedom and control, democracy and autocracy. 

1580933840448540672 2022-10-14 14:49:34+00:00 lies_das de RT @KPosmik: Wir haben uns entschieden, diese Szene von der Demo der #AfD #b0810 zu zeigen, um mit dem Vorurteil aufzuräumen, die #Luegenpr…
1580933839823679488 2022-10-14 14:49:34+00:00 JaviMac10 en RT @dim0kq: 0. I admire the actions of SpaceX of enabling StarLink service in Ukraine. It is a true game changer for Ukrainian army in the…
1580933839748145155 2022-10-14 14:49:34+00:00 LovelyLassSandy en @elonmusk Please consider continuing your Starlink platform in Ukraine! Can you work out a way to make this a charitable contribution for which you receive substantial tax breaks. Do not leave these families disconnected! Starlink has been a God to Ukraine!`

Python code:

for tweet in tweets:
    print(tweet.id, tweet.created_at, users[tweet.author_id].username, tweet.lang, tweet.text)
# Make a new file
    with open("test_file.csv", "w") as my_file:
            writer = csv.writer(my_file)
            writer.writerows(tweet.id, tweet.created_at, users[tweet.author_id].username, tweet.lang, tweet.text)

error:

 writer.writerows(tweet.id, tweet.created_at, users[tweet.author_id].username, tweet.lang, tweet.text)
TypeError: writerows() takes exactly one argument (5 given)

Solution

  • import csv
    with open("test_file.csv", "w") as my_file:
        writer = csv.writer(my_file)
        for tweet in tweets:
            writer.writerow([tweet.id, tweet.created_at,users[tweet.author_id].username, tweet.lang, tweet.text])