Writing to CSV with Python adds blank lines [duplicate] Writing to CSV with Python adds blank lines [duplicate] python python

Writing to CSV with Python adds blank lines [duplicate]


The way you use the csv module changed in Python 3 in several respects (docs), at least with respect to how you need to open the file. Anyway, something like

import csvwith open('test.csv', 'w', newline='') as fp:    a = csv.writer(fp, delimiter=',')    data = [['Me', 'You'],            ['293', '219'],            ['54', '13']]    a.writerows(data)

should work.


If you're using Python 2.x on Windows you need to change your line open('test.csv', 'w') to open('test.csv', 'wb'). That is you should open the file as a binary file.

However, as stated by others, the file interface has changed in Python 3.x.


import csvhello = [['Me','You'],['293', '219'],['13','15']]length = len(hello[0])with open('test1.csv', 'wb') as testfile:    csv_writer = csv.writer(testfile)    for y in range(length):        csv_writer.writerow([x[y] for x in hello])

will produce an output like this

Me You293 21913 15

Hope this helps