diff -r c82dcad83438 Doc/library/csv.rst --- a/Doc/library/csv.rst Fri Apr 18 17:00:50 2014 -0400 +++ b/Doc/library/csv.rst Thu Jun 26 19:22:51 2014 -0700 @@ -159,6 +159,16 @@ the optional *restval* parameter. Any other optional or keyword arguments are passed to the underlying :class:`reader` instance. + :: + + >>> 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 + .. class:: DictWriter(csvfile, fieldnames, restval='', extrasaction='raise', \ dialect='excel', *args, **kwds) @@ -181,6 +191,18 @@ objects are not ordered, there is not enough information available to deduce the order in which the row should be written to the *csvfile*. + :: + + 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'}) + .. class:: Dialect