views:

43

answers:

2

In Python 2.7 running on WinXPpro:

import csv
outfile = file('test.csv','w')
writer = csv.writer(outfile,delimiter=',',quoting=csv.QUOTE_MINIMAL)
writer.writerow(['hi','dude'])
writer.writerow(['hi2','dude2'])
outfile.close()

Generates a test.csv file with an extra \r at each row, like so:

test.csv

hi,dude\r\r\nhi2,dude2\r\r\n

instead of the expected:

hi,dude\r\nhi2,dude2\r\n

Any idea why this is happening, or if this is actually the desired behavior?

+2  A: 

I'm not sure exactly why it is happening, but changing your file mode from "w" to "wb" fixes it. See my answer to "how to remove ^M" for more details.

Ned Batchelder
+1  A: 

On Windows, always open your files in binary mode ("rb" or "wb") before passing them to csv.reader or csv.writer.

CSV is really a binary format, with "\r\n" separating records. If that separator is written in text mode, the Python runtime replaces the "\n" with "\r\n" hence the "\r\r\n" that you observed in your file.

See this previous answer.

John Machin