CSV in Python adding an extra carriage return, on Windows
--
Track title: CC P Beethoven - Piano Sonata No 2 in A
--
Chapters
00:00 Question
00:42 Accepted answer (Score 481)
01:43 Answer 2 (Score 279)
02:53 Answer 3 (Score 67)
03:42 Answer 4 (Score 13)
04:04 Thank you
--
Full question
https://stackoverflow.com/questions/3191...
Accepted answer links:
[csv]: https://docs.python.org/3/library/csv.ht...
[disable universal newlines translation]: https://docs.python.org/3/library/functi...
[lineterminator]: https://docs.python.org/3/library/csv.ht...
[RFC 4180]: https://www.rfc-editor.org/rfc/rfc4180#s...
[this previous answer]: https://stackoverflow.com/questions/1170...
Answer 2 links:
[@john-machin]: https://stackoverflow.com/a/3191811/3357...
Answer 3 links:
[documentation]: https://docs.python.org/3/library/csv.ht...
[footnote]: https://docs.python.org/3/library/csv.ht...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #windows #csv #newline
#avk47
ACCEPTED ANSWER
Score 534
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 '\r\n' 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 \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.
ANSWER 2
Score 289
While @john-machin gives a good answer, it's not always the best approach. For example, it doesn't work on Python 3 unless you encode all of your inputs to the CSV writer. Also, it doesn't address the issue if the script wants to use sys.stdout as the stream.
I suggest instead setting the 'lineterminator' attribute when creating the writer:
import csv
import sys
doc = csv.writer(sys.stdout, lineterminator='\n')
doc.writerow('abc')
doc.writerow(range(3))
That example will work on Python 2 and Python 3 and won't produce the unwanted newline characters. Note, however, that it may produce undesirable newlines (omitting the LF character on Unix operating systems).
In most cases, however, I believe that behavior is preferable and more natural than treating all CSV as a binary format. I provide this answer as an alternative for your consideration.
ANSWER 3
Score 67
In Python 3 (I haven't tried this in Python 2), you can also simply do
with open('output.csv','w',newline='') as f:
writer=csv.writer(f)
writer.writerow(mystuff)
...
as per documentation.
More on this in the doc's footnote:
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.
ANSWER 4
Score 2
You have to add attribute newline="\n" to open function like this:
with open('file.csv','w',newline="\n") as out:
csv_out = csv.writer(out, delimiter =';')