[Tutor] exporting lists into CSV issue.

Peter Otten __peter__ at web.de
Tue Jan 22 06:26:11 EST 2019


mhysnm1964 at gmail.com wrote:

> Now I am
> trying to export to a CSV file. There is no syntax or logical errors I can
> see. The information is being exported to the CSV. But when I bring it
> into Excel. I am getting a blank row between each row.

> with open ('books-list.csv', 'w') as wf:

Try switching off newline conversion:

with open('books-list.csv', 'w', newline="") as wf:
   ...

The csv.writer() delimits records with "\r\n", and on Windows open() 
converts "\n" to "\r\n" by default. Therefore you probably (no Window handy 
to to confirm) end up with "\r\r\n" between the rows.

See also <https://docs.python.org/dev/library/csv.html#id3>.

>     writer = csv.writer(wf)
>     writer.writerows(books)

At this point the file is already closed by the context manager, no need for 
an extra

> wf.close()




More information about the Tutor mailing list