summaryrefslogtreecommitdiffstats
path: root/Doc
diff options
context:
space:
mode:
authorBerker Peksag <berker.peksag@gmail.com>2014-11-24 21:50:46 (GMT)
committerBerker Peksag <berker.peksag@gmail.com>2014-11-24 21:50:46 (GMT)
commit8a9c682d41a3ad414128a9a2697542aa8c30ac11 (patch)
treeb7ea267597c59170e8255b6aa2b6e6a4426e2a7e /Doc
parent40ce014baf1595b79bf091d8eebbae3a1ac5f3a6 (diff)
downloadcpython-8a9c682d41a3ad414128a9a2697542aa8c30ac11.zip
cpython-8a9c682d41a3ad414128a9a2697542aa8c30ac11.tar.gz
cpython-8a9c682d41a3ad414128a9a2697542aa8c30ac11.tar.bz2
Issue #20351: Add examples for csv.DictReader and csv.DictWriter.
Patch by Charles-Axel Dein.
Diffstat (limited to 'Doc')
-rw-r--r--Doc/library/csv.rst25
1 files changed, 25 insertions, 0 deletions
diff --git a/Doc/library/csv.rst b/Doc/library/csv.rst
index 2054ce6..1f5ae65 100644
--- a/Doc/library/csv.rst
+++ b/Doc/library/csv.rst
@@ -180,6 +180,18 @@ The :mod:`csv` module defines the following classes:
of 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)
@@ -202,6 +214,19 @@ The :mod:`csv` module defines the following classes:
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