CSV in Python adding an extra carriage return, on Windows
CSV in Python adding an extra carriage return, on Windows
CSV in Python adding an extra carriage return, on Windows
Re: CSV in Python adding an extra carriage return, on Windows
Python 3:
The official [`csv` documentation](https://docs.python.org/3/library/csv.html#examples) recommends `open`ing the file with `newline=''` on all platforms to [disable universal newlines translation](https://docs.python.org/3/library/functions.html#open-newline-parameter):
`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](https://docs.python.org/3/library/csv.html#csv.Dialect.lineterminator), which is `'\r\n'` for the default `excel` dialect on all platforms because that's what [RFC 4180](https://www.rfc-editor.org/rfc/rfc4180#section-2) 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 `\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` observed in the file.
See [this previous answer](https://stackoverflow.com/questions/1170214/pythons-csv-writer-produces-wrong-line-terminator/1170297#1170297).
The official [`csv` documentation](https://docs.python.org/3/library/csv.html#examples) recommends `open`ing the file with `newline=''` on all platforms to [disable universal newlines translation](https://docs.python.org/3/library/functions.html#open-newline-parameter):
`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](https://docs.python.org/3/library/csv.html#csv.Dialect.lineterminator), which is `'\r\n'` for the default `excel` dialect on all platforms because that's what [RFC 4180](https://www.rfc-editor.org/rfc/rfc4180#section-2) 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 `\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` observed in the file.
See [this previous answer](https://stackoverflow.com/questions/1170214/pythons-csv-writer-produces-wrong-line-terminator/1170297#1170297).