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

Foxan Ng
  • 6,883
  • 4
  • 34
  • 41
vivi11130704
  • 431
  • 8
  • 21

3 Answers3

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])
Foxan Ng
  • 6,883
  • 4
  • 34
  • 41
  • 1
    It may or may not cause an error - but the `with` usage closes the file. Doing it manually is either erroneous or redundant. – Shadow Nov 21 '17 at 03:47
  • Thank you. However this produces an empty line between each record. Any way to eliminate the empty lines? – vivi11130704 Nov 21 '17 at 03:49
  • @vivi11130704 I don't see empty lines in my case (except the ending one). – Foxan Ng Nov 21 '17 at 03:52
  • 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. – vivi11130704 Nov 21 '17 at 03:58
  • @Shadow it won't crash, as I found out :) https://stackoverflow.com/questions/39600786/why-can-you-close-a-file-object-more-than-once – roganjosh Nov 21 '17 at 04:12
  • @vivi11130704 Can you double check by opening the csv file using text editor / other IDE? I'm also using Jupyter notebook. – Foxan Ng Nov 21 '17 at 04:14
  • @vivi11130704 open the file in `wb` mode, not `w`. On Windows, writing to CSV will include blank lines if you don't open in `wb` mode. – roganjosh Nov 21 '17 at 04:15
  • @roganjosh fair enough. Guess it's the second one then :P – Shadow Nov 21 '17 at 04:51
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, ))
Arun
  • 1,933
  • 2
  • 28
  • 46
  • 2
    That's much better :) – Shadow Nov 21 '17 at 03:46
  • Thank you. However this produces an empty line between each record. Any way to eliminate the empty lines? – vivi11130704 Nov 21 '17 at 03:49
  • 1
    No it's not generating any empty lines @vivi11130704. I just ran it – yash Nov 21 '17 at 03:51
  • @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? – Arun Nov 21 '17 at 04:16
  • 1
    To prevent empty lines, use `csvout = csv.writer(output_write, lineterminator="\n")`. [Source](https://stackoverflow.com/a/9845731/4860123) – thibautg Jul 19 '18 at 09:53
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

srikavineehari
  • 2,502
  • 1
  • 11
  • 21