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

Skip to content

Commit 711f093

Browse files
committed
#15932: merge with 3.2.
2 parents 9b1c84b + e34f8a9 commit 711f093

1 file changed

Lines changed: 15 additions & 13 deletions

File tree

Doc/library/csv.rst

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,10 @@ The :mod:`csv` module defines the following functions:
7171
A short usage example::
7272

7373
>>> import csv
74-
>>> spamReader = csv.reader(open('eggs.csv', newline=''), delimiter=' ', quotechar='|')
75-
>>> for row in spamReader:
76-
... print(', '.join(row))
74+
>>> with open('eggs.csv', newline='') as csvfile:
75+
... spamreader = csv.reader(csvfile, delimiter=' ', quotechar='|')
76+
... for row in spamreader:
77+
... print(', '.join(row))
7778
Spam, Spam, Spam, Spam, Spam, Baked Beans
7879
Spam, Lovely Spam, Wonderful Spam
7980

@@ -99,11 +100,12 @@ The :mod:`csv` module defines the following functions:
99100

100101
A short usage example::
101102

102-
>>> import csv
103-
>>> spamWriter = csv.writer(open('eggs.csv', 'w', newline=''), delimiter=' ',
104-
... quotechar='|', quoting=csv.QUOTE_MINIMAL)
105-
>>> spamWriter.writerow(['Spam'] * 5 + ['Baked Beans'])
106-
>>> spamWriter.writerow(['Spam', 'Lovely Spam', 'Wonderful Spam'])
103+
import csv
104+
with open('eggs.csv', 'w', newline='') as csvfile:
105+
spamwriter = csv.writer(csvfile, delimiter=' ',
106+
quotechar='|', quoting=csv.QUOTE_MINIMAL)
107+
spamwriter.writerow(['Spam'] * 5 + ['Baked Beans'])
108+
spamwriter.writerow(['Spam', 'Lovely Spam', 'Wonderful Spam'])
107109

108110

109111
.. function:: register_dialect(name[, dialect], **fmtparams)
@@ -221,11 +223,11 @@ The :mod:`csv` module defines the following classes:
221223

222224
An example for :class:`Sniffer` use::
223225

224-
csvfile = open("example.csv")
225-
dialect = csv.Sniffer().sniff(csvfile.read(1024))
226-
csvfile.seek(0)
227-
reader = csv.reader(csvfile, dialect)
228-
# ... process CSV file contents here ...
226+
with open('example.csv') as csvfile:
227+
dialect = csv.Sniffer().sniff(csvfile.read(1024))
228+
csvfile.seek(0)
229+
reader = csv.reader(csvfile, dialect)
230+
# ... process CSV file contents here ...
229231

230232

231233
The :mod:`csv` module defines the following constants:

0 commit comments

Comments
 (0)