class csv.DictWriter(csvfile, fieldnames, restval='', extrasaction='raise', dialect='excel', *args, **kwds)
Create an object which operates like a regular writer but maps dictionaries onto output rows. The fieldnames parameter is a sequence
of keys that identify the order in which values in the dictionary passed to the writerow()
method are written to the csvfile. The optional restval parameter specifies the value to be written if the dictionary is missing a key in fieldnames. If the dictionary passed to the writerow()
method contains a key not found in fieldnames, the optional extrasaction parameter indicates what action to take. If it is set to 'raise'
a ValueError
is raised. If it is set to 'ignore'
, extra values in the dictionary are ignored. Any other optional or keyword arguments are passed to the underlying writer
instance.
Note that unlike the DictReader
class, the fieldnames parameter of the DictWriter
is not optional. Since Python’s dict
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'})
Please login to continue.