[Python-checkins] cpython (merge 3.4 -> default): Issue #20351: Add examples for csv.DictReader and csv.DictWriter.

berker.peksag python-checkins at python.org
Mon Nov 24 22:46:58 CET 2014


https://hg.python.org/cpython/rev/c2b36196b7f5
changeset:   93577:c2b36196b7f5
parent:      93575:0ddcc455e001
parent:      93576:268ceaa78cf9
user:        Berker Peksag <berker.peksag at gmail.com>
date:        Mon Nov 24 23:47:06 2014 +0200
summary:
  Issue #20351: Add examples for csv.DictReader and csv.DictWriter.

Patch by Charles-Axel Dein.

files:
  Doc/library/csv.rst |  25 +++++++++++++++++++++++++
  1 files changed, 25 insertions(+), 0 deletions(-)


diff --git a/Doc/library/csv.rst b/Doc/library/csv.rst
--- a/Doc/library/csv.rst
+++ b/Doc/library/csv.rst
@@ -159,6 +159,18 @@
    the optional *restval* parameter.  Any other optional or keyword arguments
    are passed to the underlying :class:`reader` instance.
 
+   A short usage example::
+
+       >>> import csv
+       >>> with open('names.csv') as csvfile:
+       ...     reader = csv.DictReader(csvfile)
+       ...     for row in reader:
+       ...         print(row['first_name'], row['last_name'])
+       ...
+       Baked Beans
+       Lovely Spam
+       Wonderful Spam
+
 
 .. class:: DictWriter(csvfile, fieldnames, restval='', extrasaction='raise', \
                       dialect='excel', *args, **kwds)
@@ -181,6 +193,19 @@
    objects are not ordered, there is not enough information available to deduce
    the order in which the row should be written to the *csvfile*.
 
+   A short usage example::
+
+       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'})
+
 
 .. class:: Dialect
 

-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list