mirror of
https://github.com/python/cpython.git
synced 2025-09-27 10:50:04 +00:00
#15932: use with statement in csv doc examples.
This commit is contained in:
parent
714e64ebd1
commit
a733d814be
1 changed files with 15 additions and 13 deletions
|
@ -78,8 +78,9 @@ The :mod:`csv` module defines the following functions:
|
|||
A short usage example::
|
||||
|
||||
>>> import csv
|
||||
>>> spamReader = csv.reader(open('eggs.csv', 'rb'), delimiter=' ', quotechar='|')
|
||||
>>> for row in spamReader:
|
||||
>>> with open('eggs.csv', 'rb') as csvfile:
|
||||
... spamreader = csv.reader(csvfile, delimiter=' ', quotechar='|')
|
||||
... for row in spamreader:
|
||||
... print ', '.join(row)
|
||||
Spam, Spam, Spam, Spam, Spam, Baked Beans
|
||||
Spam, Lovely Spam, Wonderful Spam
|
||||
|
@ -115,11 +116,12 @@ The :mod:`csv` module defines the following functions:
|
|||
|
||||
A short usage example::
|
||||
|
||||
>>> import csv
|
||||
>>> spamWriter = csv.writer(open('eggs.csv', 'wb'), delimiter=' ',
|
||||
... quotechar='|', quoting=csv.QUOTE_MINIMAL)
|
||||
>>> spamWriter.writerow(['Spam'] * 5 + ['Baked Beans'])
|
||||
>>> spamWriter.writerow(['Spam', 'Lovely Spam', 'Wonderful Spam'])
|
||||
import csv
|
||||
with open('eggs.csv', 'wb') as csvfile:
|
||||
spamwriter = csv.writer(csvfile, delimiter=' ',
|
||||
quotechar='|', quoting=csv.QUOTE_MINIMAL)
|
||||
spamwriter.writerow(['Spam'] * 5 + ['Baked Beans'])
|
||||
spamwriter.writerow(['Spam', 'Lovely Spam', 'Wonderful Spam'])
|
||||
|
||||
|
||||
.. function:: register_dialect(name[, dialect], **fmtparams)
|
||||
|
@ -234,7 +236,7 @@ The :mod:`csv` module defines the following classes:
|
|||
|
||||
An example for :class:`Sniffer` use::
|
||||
|
||||
csvfile = open("example.csv", "rb")
|
||||
with open('example.csv', 'rb') as csvfile:
|
||||
dialect = csv.Sniffer().sniff(csvfile.read(1024))
|
||||
csvfile.seek(0)
|
||||
reader = csv.reader(csvfile, dialect)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue