143
143
The :mod:`csv` module defines the following classes:
145
.. class:: DictReader(csvfile, fieldnames=None, restkey=None, restval=None, dialect='excel', *args, **kwds)
147
Create an object which operates like a regular reader but maps the information
148
read into a dict whose keys are given by the optional *fieldnames* parameter.
149
If the *fieldnames* parameter is omitted, the values in the first row of the
150
*csvfile* will be used as the fieldnames. If the row read has more fields
151
than the fieldnames sequence, the remaining data is added as a sequence
152
keyed by the value of *restkey*. If the row read has fewer fields than the
153
fieldnames sequence, the remaining keys take the value of the optional
154
*restval* parameter. Any other optional or keyword arguments are passed to
155
the underlying :class:`reader` instance.
158
.. class:: DictWriter(csvfile, fieldnames, restval='', extrasaction='raise', dialect='excel', *args, **kwds)
160
Create an object which operates like a regular writer but maps dictionaries onto
161
output rows. The *fieldnames* parameter identifies the order in which values in
162
the dictionary passed to the :meth:`writerow` method are written to the
163
*csvfile*. The optional *restval* parameter specifies the value to be written
164
if the dictionary is missing a key in *fieldnames*. If the dictionary passed to
165
the :meth:`writerow` method contains a key not found in *fieldnames*, the
166
optional *extrasaction* parameter indicates what action to take. If it is set
167
to ``'raise'`` a :exc:`ValueError` is raised. If it is set to ``'ignore'``,
168
extra values in the dictionary are ignored. Any other optional or keyword
169
arguments are passed to the underlying :class:`writer` instance.
171
Note that unlike the :class:`DictReader` class, the *fieldnames* parameter of
172
the :class:`DictWriter` is not optional. Since Python's :class:`dict` objects
173
are not ordered, there is not enough information available to deduce the order
174
in which the row should be written to the *csvfile*.
145
.. class:: DictReader(csvfile, fieldnames=None, restkey=None, restval=None, \
146
dialect='excel', *args, **kwds)
148
Create an object which operates like a regular reader but maps the
149
information read into a dict whose keys are given by the optional
150
*fieldnames* parameter. The *fieldnames* parameter is a :mod:`sequence
151
<collections.abc>` whose elements are associated with the fields of the
152
input data in order. These elements become the keys of the resulting
153
dictionary. If the *fieldnames* parameter is omitted, the values in the
154
first row of the *csvfile* will be used as the fieldnames. If the row read
155
has more fields than the fieldnames sequence, the remaining data is added as
156
a sequence keyed by the value of *restkey*. If the row read has fewer
157
fields than the fieldnames sequence, the remaining keys take the value of
158
the optional *restval* parameter. Any other optional or keyword arguments
159
are passed to the underlying :class:`reader` instance.
162
.. class:: DictWriter(csvfile, fieldnames, restval='', extrasaction='raise', \
163
dialect='excel', *args, **kwds)
165
Create an object which operates like a regular writer but maps dictionaries
166
onto output rows. The *fieldnames* parameter is a :mod:`sequence
167
<collections.abc>` of keys that identify the order in which values in the
168
dictionary passed to the :meth:`writerow` method are written to the
169
*csvfile*. The optional *restval* parameter specifies the value to be
170
written if the dictionary is missing a key in *fieldnames*. If the
171
dictionary passed to the :meth:`writerow` method contains a key not found in
172
*fieldnames*, the optional *extrasaction* parameter indicates what action to
173
take. If it is set to ``'raise'`` a :exc:`ValueError` is raised. If it is
174
set to ``'ignore'``, extra values in the dictionary are ignored. Any other
175
optional or keyword arguments are passed to the underlying :class:`writer`
178
Note that unlike the :class:`DictReader` class, the *fieldnames* parameter
179
of the :class:`DictWriter` is not optional. Since Python's :class:`dict`
180
objects are not ordered, there is not enough information available to deduce
181
the order in which the row should be written to the *csvfile*.
177
184
.. class:: Dialect