Python3: writing csv files
Asked Answered
P

6

42

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 try spamWriter.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?

Player answered 26/8, 2011 at 6:5 Comment(1)
The problem seems to be Windows-specific. Works fine on Linux.Spume
M
68

Documentation says that you should use open('eggs.csv', 'w', newline='')

http://docs.python.org/py3k/library/csv.html#id2

Mall answered 26/8, 2011 at 6:49 Comment(1)
Ah, it seems I picked up a slightly older version (-0.1) of the documentation (compare 3.2 vs 3.2.1)Player
P
20

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')
Personality answered 27/6, 2012 at 21:59 Comment(0)
S
7

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'])
Spume answered 26/8, 2011 at 6:49 Comment(1)
I get a TypeError: 'newline' is an invalid keyword argument for this function when trying to do this in py34.Airdrie
K
5

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.

Kellerman answered 26/8, 2011 at 6:18 Comment(3)
This is the only answer here that worked for me. And five years later the examples still don't work out-of-the-box :-(Azurite
This is the only answer works for me on Python 3.7, none of th above workedSusy
This works for me also, where newline='' did not.Rosariarosario
C
1

[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"])
Clova answered 3/10, 2014 at 4:54 Comment(1)
This is not an answer for Python 3, which raises "TypeError: 'str' does not support the buffer interface"Player
G
1

I resolved this error as I was moving one of my codes from Python2.6.6 to python3.4.3

Python2.6.6(I'm trying to do some obfuscation on my csvfile)

with open( os.path.join(path, name) , 'r') as mycsvfile:
    writer = csv.writer(open(newFilename, 'w'))

Above worked well with python2.6.6 but did not work on python3.4.3 as I was getting some utf-8 unicode errors when i tried to run the python3 file, so I made the below changes Python3.4.4

import codecs
with codecs.open(os.path.join(path, name) , 'r', encoding='ISO-8859-1') as mycsvfile:
    writer = csv.writer(open(newFilename, 'w'))

That's it, my code works fine now, basically python3 is not considering some of the unicode and we need to use the codecs import to make it working, hope it helps..

Geoffrey answered 23/11, 2016 at 19:2 Comment(1)
What is mycsvfile and what about indentation?Gittel

© 2022 - 2024 — McMap. All rights reserved.