Title: csv module examples miss newline='' when opening files
Type: enhancement Stage: patch review
Components: Documentation Versions: Python 3.7
Status: open Resolution:
Dependencies: Superseder:
Assigned To: docs@python Nosy List: Pavel, docs@python
Priority: normal Keywords: easy, patch

Created on 2017-07-15 14:48 by Pavel, last changed 2017-07-16 05:50 by ammar2.

Pull Requests
URL Status Linked Edit
PR 2730 open ammar2, 2017-07-16 05:49
Messages (1)
msg298397 - (view) Author: Pavel (Pavel) Date: 2017-07-15 14:48
At the very beginning the csv module documentation ( advises to open files passing newline='' parameter though three examples don't include it:

Here are the examples:

>>> 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')])

import csv

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

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

with open('example.csv') as csvfile:
    dialect = csv.Sniffer().sniff(
    reader = csv.reader(csvfile, dialect)
    # ... process CSV file contents here ...
Date User Action Args
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