Asked  7 Months ago    Answers:  5   Viewed   54 times
import csv

with open('test.csv', 'w') as outfile:
    writer = csv.writer(outfile, delimiter=',', quoting=csv.QUOTE_MINIMAL)
    writer.writerow(['hi', 'dude'])
    writer.writerow(['hi2', 'dude2'])

The above code generates a file, test.csv, with an extra r at each row, like so:

hi,duderrnhi2,dude2rrn

instead of the expected

hi,dudernhi2,dude2rn

Why is this happening, or is this actually the desired behavior?

 Answers

94

Python 3:

The official csv documentation recommends opening the file with newline='' on all platforms to disable universal newlines translation:

with open('output.csv', 'w', newline='', encoding='utf-8') as f:
    writer = csv.writer(f)
    ...

The CSV writer terminates each line with the lineterminator of the dialect, which is 'rn' for the default excel dialect on all platforms because that's what RFC 4180 recommends.


Python 2:

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

Although the file is a text file, CSV is regarded a binary format by the libraries involved, with rn separating records. If that separator is written in text mode, the Python runtime replaces the n with rn, hence the rrn observed in the file.

See this previous answer.

Tuesday, June 1, 2021
 
cyber_truite
answered 7 Months ago
38

In Python 2, open outfile with mode 'wb' instead of 'w'. The csv.writer writes rn into the file directly. If you don't open the file in binary mode, it will write rrn because on Windows text mode will translate each n into rn.

In Python 3 the required syntax changed (see documentation links below), so open outfile with the additional parameter newline='' (empty string) instead.

Examples:

# Python 2
with open('/pythonwork/thefile_subset11.csv', 'wb') as outfile:
    writer = csv.writer(outfile)

# Python 3
with open('/pythonwork/thefile_subset11.csv', 'w', newline='') as outfile:
    writer = csv.writer(outfile)

Documentation Links

  • https://docs.python.org/2/library/csv.html#csv.writer
  • https://docs.python.org/3/library/csv.html#csv.writer
Tuesday, June 1, 2021
 
Magnanimity
answered 7 Months ago
93

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

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

Wednesday, June 23, 2021
 
Sujith
answered 6 Months ago
29

n is converted to os.linesep for files opened in text-mode. So when you write os.linesep to a text-mode file on Windows, you write rn, and the n gets converted resulting in rrn.

See also the docs:

Do not use os.linesep as a line terminator when writing files opened in text mode (the default); use a single 'n' instead, on all platforms.

Thursday, July 22, 2021
 
ericstumper
answered 5 Months ago
29

Default line terminator for csv.writer is 'rn'. Explicitly specify lineterminator argument if you want only 'n':

wr = csv.writer(csvFile, delimiter=';', lineterminator='n')
Sunday, November 7, 2021
 
ryuikuya
answered 1 Month ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :
 
Share