[docs] Add doc examples for DictReader and DictWriter (issue 20351)

charlesaxel.dein at gmail.com charlesaxel.dein at gmail.com
Sat Apr 19 03:49:28 CEST 2014


Reviewers: francismb, vadmium, maatt,


https://bugs.python.org/review/20351/diff/11072/Doc/library/csv.rst
File Doc/library/csv.rst (right):

https://bugs.python.org/review/20351/diff/11072/Doc/library/csv.rst#newcode163
Doc/library/csv.rst:163: ...         print(row['first_name'],
row['last_name'].upper())
Sure! Fixed.



Please review this at https://bugs.python.org/review/20351/

Affected files:
  Doc/library/csv.rst


diff -r 9a98ff4a2290 Doc/library/csv.rst
--- a/Doc/library/csv.rst	Wed Jan 22 05:49:11 2014 -0800
+++ b/Doc/library/csv.rst	Wed Jan 22 10:05:24 2014 -0800
@@ -154,6 +154,17 @@
    *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'].upper())
+       Baked BEANS
+       Lovely SPAM
+       Wonderful SPAM
+
 
 .. class:: DictWriter(csvfile, fieldnames, restval='', extrasaction='raise', dialect='excel', *args, **kwds)
 
@@ -173,6 +184,19 @@
    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
 




More information about the docs mailing list