CSV file written with Python has blank lines between each row
In Python 2, open
outfile with mode
'wb' instead of
\r\n into the file directly. If you don't open the file in binary mode, it will write
\r\r\n because on Windows text mode will translate each
In Python 3 the required syntax changed (see documentation links below), so open
outfile with the additional parameter
newline='' (empty string) instead.
# Python 2with open('/pythonwork/thefile_subset11.csv', 'wb') as outfile: writer = csv.writer(outfile)# Python 3with open('/pythonwork/thefile_subset11.csv', 'w', newline='') as outfile: writer = csv.writer(outfile)
Opening the file in binary mode "wb" will not work in Python 3+. Or rather, you'd have to convert your data to binary before writing it. That's just a hassle.
Instead, you should keep it in text mode, but override the newline as empty. Like so:
with open('/pythonwork/thefile_subset11.csv', 'w', newline='') as outfile:
The simple answer is that csv files should always be opened in binary mode whether for input or output, as otherwise on Windows there are problems with the line ending. Specifically on output the csv module will write
\r\n (the standard CSV row terminator) and then (in text mode) the runtime will replace the
\r\n (the Windows standard line terminator) giving a result of
Fiddling with the
lineterminator is NOT the solution.