Commit a733d814 authored by Ezio Melotti's avatar Ezio Melotti

#15932: use with statement in csv doc examples.

parent 714e64eb
...@@ -78,9 +78,10 @@ The :mod:`csv` module defines the following functions: ...@@ -78,9 +78,10 @@ The :mod:`csv` module defines the following functions:
A short usage example:: A short usage example::
>>> import csv >>> import csv
>>> spamReader = csv.reader(open('eggs.csv', 'rb'), delimiter=' ', quotechar='|') >>> with open('eggs.csv', 'rb') as csvfile:
>>> for row in spamReader: ... spamreader = csv.reader(csvfile, delimiter=' ', quotechar='|')
... print ', '.join(row) ... for row in spamreader:
... print ', '.join(row)
Spam, Spam, Spam, Spam, Spam, Baked Beans Spam, Spam, Spam, Spam, Spam, Baked Beans
Spam, Lovely Spam, Wonderful Spam Spam, Lovely Spam, Wonderful Spam
...@@ -115,11 +116,12 @@ The :mod:`csv` module defines the following functions: ...@@ -115,11 +116,12 @@ The :mod:`csv` module defines the following functions:
A short usage example:: A short usage example::
>>> import csv import csv
>>> spamWriter = csv.writer(open('eggs.csv', 'wb'), delimiter=' ', with open('eggs.csv', 'wb') as csvfile:
... quotechar='|', quoting=csv.QUOTE_MINIMAL) spamwriter = csv.writer(csvfile, delimiter=' ',
>>> spamWriter.writerow(['Spam'] * 5 + ['Baked Beans']) quotechar='|', quoting=csv.QUOTE_MINIMAL)
>>> spamWriter.writerow(['Spam', 'Lovely Spam', 'Wonderful Spam']) spamwriter.writerow(['Spam'] * 5 + ['Baked Beans'])
spamwriter.writerow(['Spam', 'Lovely Spam', 'Wonderful Spam'])
.. function:: register_dialect(name[, dialect], **fmtparams) .. function:: register_dialect(name[, dialect], **fmtparams)
...@@ -234,11 +236,11 @@ The :mod:`csv` module defines the following classes: ...@@ -234,11 +236,11 @@ The :mod:`csv` module defines the following classes:
An example for :class:`Sniffer` use:: 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)) dialect = csv.Sniffer().sniff(csvfile.read(1024))
csvfile.seek(0) csvfile.seek(0)
reader = csv.reader(csvfile, dialect) reader = csv.reader(csvfile, dialect)
# ... process CSV file contents here ... # ... process CSV file contents here ...
The :mod:`csv` module defines the following constants: The :mod:`csv` module defines the following constants:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment