Python3: writing csv files
Solution 1
Documentation says that you should use open('eggs.csv', 'w', newline='')
http://docs.python.org/py3k/library/csv.html#id2
Solution 2
This will work on both Python 2 and Python 3:
if sys.version_info >= (3,0,0):
f = open(filename, 'w', newline='')
else:
f = open(filename, 'wb')
Solution 3
As documented in a footnote:
csv.writer(csvfile, dialect='excel', **fmtparams)
If csvfile is a file object, it should be opened with newline=''.
If newline='' is not specified, newlines embedded inside quoted fields will not be interpreted correctly, and on platforms that use \r\n linendings on write an extra \r will be added. It should always be safe to specify newline='', since the csv module does its own (universal) newline handling.
The following variant works on Linux and Windows:
spamWriter = csv.writer(open('eggs.csv', 'wb'), delimiter=' ', quotechar='|',
quoting=csv.QUOTE_MINIMAL, newline='')
spamWriter.writerow(['Spam'] * 5 + ['Baked Beans'])
spamWriter.writerow(['Spam', 'Lovely Spam', 'Wonderful Spam'])
Solution 4
To directly answer your question, you should be able to use the lineterminator formatting parameter:
...so modifying this line should work (untested):
>>> spamWriter = csv.writer(open('eggs.csv', 'w'), delimiter=' ',
... quotechar='|', quoting=csv.QUOTE_MINIMAL, lineterminator='\n')
As for why the example doesn't work out-of-the box, kind of looks like a bug to me.
Solution 5
[For Python 2.x] This implementation of spamWriter is working for me...
with open('assignmentresults.csv', 'wb') as csvfile:
spamwriter = csv.writer(csvfile, delimiter=',', quotechar='|', quoting=csv.QUOTE_MINIMAL)
spamwriter.writerow(["Hullo", "World"])
Mike T
Hydrogeologist, numerical modeller and GIS professional. My main programming languages that I use are Python, R, SQL. I dabble with Fortran and C/C++/C# on occasions. Thanks to anyone that has helped me!
Updated on July 09, 2022Comments
-
Mike T almost 2 years
I'm trying to use Python 3.2 on a Windows computer to write a simple CSV file, however I'm having no luck. From the csv module documentation for Python 3.2:
>>> import csv >>> spamWriter = csv.writer(open('eggs.csv', 'w'), delimiter=' ', ... quotechar='|', quoting=csv.QUOTE_MINIMAL) >>> spamWriter.writerow(['Spam'] * 5 + ['Baked Beans']) >>> spamWriter.writerow(['Spam', 'Lovely Spam', 'Wonderful Spam'])
produces a file with each line terminated by the byte sequence
\r\r\n
, so it looks like each line has an extra empty line when you open it with, e.g., MS Excel. This is not a "CSV file".Note, if I try the same example for Python 2.7 in Python 3.2 (where the big difference is
'w'
vs'wb'
for the file mode), I get an error when I tryspamWriter.writerow
:Traceback (most recent call last): File "", line 1, in TypeError: 'str' does not support the buffer interface
How do I write a simple CSV file from Python 3.2 on a Windows computer?
-
Mike T over 12 years
-
Mike T over 9 yearsThis is not an answer for Python 3, which raises "TypeError: 'str' does not support the buffer interface"
-
Andy Hayden over 9 yearsI get a
TypeError: 'newline' is an invalid keyword argument for this function
when trying to do this in py34. -
Rup over 7 yearsThis is the only answer here that worked for me. And five years later the examples still don't work out-of-the-box :-(
-
Roko C. Buljan about 6 yearsWhat is
mycsvfile
and what about indentation? -
Diego Andrés Díaz Espinoza over 2 yearsThis is the only answer works for me on Python 3.7, none of th above worked