8000 #7198: add newlines='' to csv.writer docs. · python/cpython@9188702 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9188702

Browse files
committed
#7198: add newlines='' to csv.writer docs.
1 parent 72bd8b7 commit 9188702

File tree

2 files changed

+11
-26
lines changed

2 files changed

+11
-26
lines changed

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'])
@@ -427,15 +428,15 @@ The simplest example of reading a CSV file::
427428
Reading a file with an alternate format::
428429

429430
import csv
430-
with open('passwd') as f:
431+
with open('passwd', newline='') as f:
431432
reader = csv.reader(f, delimiter=':', quoting=csv.QUOTE_NONE)
432433
for row in reader:
433434
print(row)
434435

435436
The corresponding simplest possible writing example is::
436437

437438
import csv
438-
with open('some.csv', 'w') as f:
439+
with open('some.csv', 'w', newline='') as f:
439440
writer = csv.writer(f)
440441
writer.writerows(someiterable)
441442

@@ -457,7 +458,7 @@ Registering a new dialect::
457458

458459
import csv
459460
csv.register_dialect('unixpwd', delimiter=':', quoting=csv.QUOTE_NONE)
460-
with open('passwd') as f:
461+
with open('passwd', newline='') as f:
461462
reader = csv.reader(f, 'unixpwd')
462463

463464
A slightly more advanced use of the reader --- catching and reporting errors::
@@ -482,7 +483,7 @@ done::
482483

483484
.. rubric:: Footnotes
484485

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

Tools/demo/README

Lines changed: 0 additions & 16 deletions
This file was deleted.

0 commit comments

Comments
 (0)
0