@@ -52,7 +52,7 @@ The :mod:`csv` module defines the following functions:
52
52
*csvfile * can be any object which supports the :term: `iterator ` protocol and returns a
53
53
string each time its :meth: `!__next__ ` method is called --- :term: `file objects
54
54
<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
56
56
*dialect * parameter can be given which is used to define a set of parameters
57
57
specific to a particular CSV dialect. It may be an instance of a subclass of
58
58
the :class: `Dialect ` class or one of the strings returned by the
@@ -79,7 +79,8 @@ The :mod:`csv` module defines the following functions:
79
79
80
80
Return a writer object responsible for converting the user's data into delimited
81
81
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 *
83
84
parameter can be given which is used to define a set of parameters specific to a
84
85
particular CSV dialect. It may be an instance of a subclass of the
85
86
:class: `Dialect ` class or one of the strings returned by the
@@ -96,7 +97,7 @@ The :mod:`csv` module defines the following functions:
96
97
A short usage example::
97
98
98
99
>>> import csv
99
- >>> spamWriter = csv.writer(open('eggs.csv', 'w'), delimiter=' ',
100
+ >>> spamWriter = csv.writer(open('eggs.csv', 'w', newline='' ), delimiter=' ',
100
101
... quotechar='|', quoting=csv.QUOTE_MINIMAL)
101
102
>>> spamWriter.writerow(['Spam'] * 5 + ['Baked Beans'])
102
103
>>> spamWriter.writerow(['Spam', 'Lovely Spam', 'Wonderful Spam'])
@@ -427,15 +428,15 @@ The simplest example of reading a CSV file::
427
428
Reading a file with an alternate format::
428
429
429
430
import csv
430
- with open('passwd') as f:
431
+ with open('passwd', newline='' ) as f:
431
432
reader = csv.reader(f, delimiter=':', quoting=csv.QUOTE_NONE)
432
433
for row in reader:
433
434
print(row)
434
435
435
436
The corresponding simplest possible writing example is::
436
437
437
438
import csv
438
- with open('some.csv', 'w') as f:
439
+ with open('some.csv', 'w', newline='' ) as f:
439
440
writer = csv.writer(f)
440
441
writer.writerows(someiterable)
441
442
@@ -457,7 +458,7 @@ Registering a new dialect::
457
458
458
459
import csv
459
460
csv.register_dialect('unixpwd', delimiter=':', quoting=csv.QUOTE_NONE)
460
- with open('passwd') as f:
461
+ with open('passwd', newline='' ) as f:
461
462
reader = csv.reader(f, 'unixpwd')
462
463
463
464
A slightly more advanced use of the reader --- catching and reporting errors::
@@ -482,7 +483,7 @@ done::
482
483
483
484
.. rubric :: Footnotes
484
485
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 .
0 commit comments