2

I have this list of countries:

country = ['Togo', 'Nauru', 'Palestine, State of', 'Malawi']

I'm trying to write this into a csv:

with open('temp.csv', 'wt') as output_write:
    csvout = csv.writer(output_write)
    csvout.writerow(country)
    output_write.close()

However the output puts the values into a row rather than a column in csv. Can someone please let me know how to change it?

Thanks in advance!


I followed some of the suggestions below and the output has empty lines between rows:

enter image description here

The code I used:

import csv
country = ['Togo', 'Nauru', 'Palestine, State of', 'Malawi']
with open('temp.csv', 'wt') as output_write:
    csvout = csv.writer(output_write)
    for item in country:
        csvout.writerow((item, ))

Update:

I figured the reason that I'm getting an empty line because each row is because windows interpret a new line differently. The code that finally work for me is:

import csv
country = ['Togo', 'Nauru', 'Palestine, State of', 'Malawi']
with open('temp.csv', 'w', newline = '') as output_write:
    csvout = csv.writer(output_write)
    for item in country:
        csvout.writerow((item, ))

Found a related post regarding the empty row:

python empty row

2
  • 1
    The name of the function you are calling is writerow. Why do you expect that to write a column? Commented Nov 21, 2017 at 3:36
  • I agree with @Shadow, the function's name is clearly that it write a row rather than column Commented Nov 21, 2017 at 3:40

3 Answers 3

2

You have to iterate through the list if you want to write each item to a separate line:

import csv

country = ['Togo', 'Nauru', 'Palestine, State of', 'Malawi']

with open('temp.csv', 'wt') as output_write:
    csvout = csv.writer(output_write, delimiter=',')
    for c in country:
         csvout.writerow([c])
Sign up to request clarification or add additional context in comments.

8 Comments

It may or may not cause an error - but the with usage closes the file. Doing it manually is either erroneous or redundant.
Thank you. However this produces an empty line between each record. Any way to eliminate the empty lines?
@vivi11130704 I don't see empty lines in my case (except the ending one).
I update my original post to include a screenshot of what I'm getting. Not sure why I'm getting different results than everyone else. I'm using jupyter notebook.
@Shadow it won't crash, as I found out :) stackoverflow.com/questions/39600786/…
|
1

Try this:

import csv

countries = ['Togo', 'Nauru', 'Palestine, State of', 'Malawi']

with open('temp.csv', 'w') as output_write:
    csvout = csv.writer(output_write, lineterminator='\n')
    for country in countries:
         csvout.writerow([country])

enter image description here

Comments

1

Try the following:

country = ['Togo', 'Nauru', 'Palestine, State of', 'Malawi']
with open('temp.csv', 'wt') as output_write:
    csvout = csv.writer(output_write, lineterminator='\n')
    for item in country:
        csvout.writerow((item, ))

5 Comments

That's much better :)
Thank you. However this produces an empty line between each record. Any way to eliminate the empty lines?
No it's not generating any empty lines @vivi11130704. I just ran it
@vivi11130704: I have run the code in Jupyter notebook. The csv generated does not have empty lines. Maybe the problem of your csv reader? Can you open generated CSV file in a plain text editor?
To prevent empty lines, use csvout = csv.writer(output_write, lineterminator="\n"). Source

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.