classification
Title: csv module examples miss newline='' when opening files
Type: behavior Stage: resolved
Components: Documentation Versions: Python 3.7, Python 3.6
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: docs@python Nosy List: Pavel, berker.peksag, docs@python
Priority: normal Keywords: easy, patch

Created on 2017-07-15 14:48 by Pavel, last changed 2017-10-26 08:39 by berker.peksag. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 2730 merged ammar2, 2017-07-16 05:49
PR 4129 merged python-dev, 2017-10-26 08:27
Messages (4)
msg298397 - (view) Author: Pavel (Pavel) Date: 2017-07-15 14:48
At the very beginning the csv module documentation (https://docs.python.org/3.7/library/csv.html) advises to open files passing newline='' parameter though three examples don't include it:

Here are the examples:

1:
>>> import csv
>>> with open('names.csv') as csvfile:
...     reader = csv.DictReader(csvfile)
...     for row in reader:
...         print(row['first_name'], row['last_name'])
...
Eric Idle
John Cleese

>>> print(row)
OrderedDict([('first_name', 'John'), ('last_name', 'Cleese')])

2:
import csv

with open('names.csv', 'w') as csvfile:
    fieldnames = ['first_name', 'last_name']
    writer = csv.DictWriter(csvfile, fieldnames=fieldnames)

    writer.writeheader()
    writer.writerow({'first_name': 'Baked', 'last_name': 'Beans'})
    writer.writerow({'first_name': 'Lovely', 'last_name': 'Spam'})
    writer.writerow({'first_name': 'Wonderful', 'last_name': 'Spam'})

3:
with open('example.csv') as csvfile:
    dialect = csv.Sniffer().sniff(csvfile.read(1024))
    csvfile.seek(0)
    reader = csv.reader(csvfile, dialect)
    # ... process CSV file contents here ...
msg305035 - (view) Author: Berker Peksag (berker.peksag) * (Python committer) Date: 2017-10-26 08:27
New changeset 275d2d9c4663a1ea8d1f7c8778567a735b0372c1 by Berker Peksag (Ammar Askar) in branch 'master':
bpo-30937: Make usage of newline='' consistent in csv docs (GH-2730)
https://github.com/python/cpython/commit/275d2d9c4663a1ea8d1f7c8778567a735b0372c1
msg305037 - (view) Author: Berker Peksag (berker.peksag) * (Python committer) Date: 2017-10-26 08:38
New changeset 614ea48986f80d361043510ac3945f3dcd666c31 by Berker Peksag (Miss Islington (bot)) in branch '3.6':
bpo-30937: Make usage of newline='' consistent in csv docs (GH-2730)
https://github.com/python/cpython/commit/614ea48986f80d361043510ac3945f3dcd666c31
msg305038 - (view) Author: Berker Peksag (berker.peksag) * (Python committer) Date: 2017-10-26 08:39
Good catch, Pavel. Thanks! This is now fixed in 3.6 and 3.7 docs. Thanks for the patch, Ammar.
History
Date User Action Args
2017-10-26 08:39:50berker.peksagsetstatus: open -> closed
versions: + Python 3.6
type: enhancement -> behavior
messages: + msg305038

resolution: fixed
stage: patch review -> resolved
2017-10-26 08:38:19berker.peksagsetmessages: + msg305037
2017-10-26 08:27:54python-devsetpull_requests: + pull_request4094
2017-10-26 08:27:48berker.peksagsetnosy: + berker.peksag
messages: + msg305035
2017-07-16 05:50:47ammar2setkeywords: + patch, easy
type: enhancement
stage: patch review
2017-07-16 05:49:40ammar2setpull_requests: + pull_request2790
2017-07-15 14:48:05Pavelcreate