I am trying to write a nested list into a csv file, using Python 2.7.
I had encountered encoding issues,
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe5' in position 14: ordinal not in range(128)
which I solved using this post: Read and Write CSV files including unicode with Python 2.7
This code works fine encoding-wise, but the generated file has empty lines between all its lines:
def write_to_file (list_to_write):
with open(basename_noext+'.csv', 'a') as thefile:
writer=csv.writer(thefile)
for row in list_to_write:
row=[s.encode('utf-8') for s in row]
writer.writerows([row])
If I look in the file, I see an CRLF
and a CR
at the end of each line.
Solution to double lines:
open the file as binary for append 'ab'
:
with open(basename_noext+'.csv', 'ab') as thefile:
Edited the question to provide the full working code; after finding the answer to the initial question in the duplicate question, I found another bug in the code; I added the solution to this as well