Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f453deb

Browse files
committed
#7198: really add newline='' to csv.writer docs.
Changeset ab27f16f707a was messed up by a rebase (as were 959f666470cc and 9d1b1a95bc8f) and the patch only got applied to default.
1 parent 1b407fe commit f453deb

1 file changed

Lines changed: 11 additions & 10 deletions

File tree

Doc/library/csv.rst

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ The :mod:`csv` module defines the following functions:
5252
*csvfile* can be any object which supports the :term:`iterator` protocol and returns a
5353
string each time its :meth:`!__next__` method is called --- :term:`file objects
5454
<file object>` and list objects are both suitable. If *csvfile* is a file object,
55-
it should be opened with ``newline=''``. [#]_ An optional
55+
it should be opened with ``newline=''``. [1]_ An optional
5656
*dialect* parameter can be given which is used to define a set of parameters
5757
specific to a particular CSV dialect. It may be an instance of a subclass of
5858
the :class:`Dialect` class or one of the strings returned by the
@@ -79,7 +79,8 @@ The :mod:`csv` module defines the following functions:
7979

8080
Return a writer object responsible for converting the user's data into delimited
8181
strings on the given file-like object. *csvfile* can be any object with a
82-
:func:`write` method. An optional *dialect*
82+
:func:`write` method. If csvfile is a file object, it should be opened with
83+
newline='' [1]_. An optional *dialect*
8384
parameter can be given which is used to define a set of parameters specific to a
8485
particular CSV dialect. It may be an instance of a subclass of the
8586
:class:`Dialect` class or one of the strings returned by the
@@ -96,7 +97,7 @@ The :mod:`csv` module defines the following functions:
9697
A short usage example::
9798

9899
>>> import csv
99-
>>> spamWriter = csv.writer(open('eggs.csv', 'w'), delimiter=' ',
100+
>>> spamWriter = csv.writer(open('eggs.csv', 'w', newline=''), delimiter=' ',
100101
... quotechar='|', quoting=csv.QUOTE_MINIMAL)
101102
>>> spamWriter.writerow(['Spam'] * 5 + ['Baked Beans'])
102103
>>> spamWriter.writerow(['Spam', 'Lovely Spam', 'Wonderful Spam'])
@@ -408,15 +409,15 @@ The simplest example of reading a CSV file::
408409
Reading a file with an alternate format::
409410

410411
import csv
411-
with open('passwd') as f:
412+
with open('passwd', newline='') as f:
412413
reader = csv.reader(f, delimiter=':', quoting=csv.QUOTE_NONE)
413414
for row in reader:
414415
print(row)
415416

416417
The corresponding simplest possible writing example is::
417418

418419
import csv
419-
with open('some.csv', 'w') as f:
420+
with open('some.csv', 'w', newline='') as f:
420421
writer = csv.writer(f)
421422
writer.writerows(someiterable)
422423

@@ -438,7 +439,7 @@ Registering a new dialect::
438439

439440
import csv
440441
csv.register_dialect('unixpwd', delimiter=':', quoting=csv.QUOTE_NONE)
441-
with open('passwd') as f:
442+
with open('passwd', newline='') as f:
442443
reader = csv.reader(f, 'unixpwd')
443444

444445
A slightly more advanced use of the reader --- catching and reporting errors::
@@ -463,7 +464,7 @@ done::
463464

464465
.. rubric:: Footnotes
465466

466-
.. [#] If ``newline=''`` is not specified, newlines embedded inside quoted fields
467-
will not be interpreted correctly. It should always be safe to specify
468-
``newline=''``, since the csv module does its own universal newline handling
469-
on input.
467+
.. [1] If ``newline=''`` is not specified, newlines embedded inside quoted fields
468+
will not be interpreted correctly, and on platforms that use ``\r\n`` linendings
469+
on write an extra `\\r` will be added. It should always be safe to specify
470+
``newline=''``, since the csv module does its own (universal) newline handling.

0 commit comments

Comments
 (0)