diff --git a/Doc/library/csv.rst b/Doc/library/csv.rst index 7fb4fc8..559a122 100644 --- a/Doc/library/csv.rst +++ b/Doc/library/csv.rst @@ -150,12 +150,15 @@ The :mod:`csv` module defines the following classes: dialect='excel', *args, **kwds) Create an object which operates like a regular reader but maps the - information read into a dict whose keys are given by the optional + information read into an :class:`OrderedDict` whose keys are given by the optional *fieldnames* parameter. The *fieldnames* parameter is a :mod:`sequence ` whose elements are associated with the fields of the input data in order. These elements become the keys of the resulting - dictionary. If the *fieldnames* parameter is omitted, the values in the - first row of the *csvfile* will be used as the fieldnames. If the row read + dictionary, with the ordering in which they appear in *fieldnames*. + + If the *fieldnames* parameter is omitted, the values in the + first row of the *csvfile* will be used as the fieldnames, in the order + that they appear in that row. If the row read has more fields than the fieldnames sequence, the remaining data is added as a sequence keyed by the value of *restkey*. If the row read has fewer fields than the fieldnames sequence, the remaining keys take the value of diff --git a/Lib/csv.py b/Lib/csv.py index 90461db..2e2303a 100644 --- a/Lib/csv.py +++ b/Lib/csv.py @@ -11,6 +11,7 @@ from _csv import Error, __version__, writer, reader, register_dialect, \ __doc__ from _csv import Dialect as _Dialect +from collections import OrderedDict from io import StringIO __all__ = ["QUOTE_MINIMAL", "QUOTE_ALL", "QUOTE_NONNUMERIC", "QUOTE_NONE", @@ -116,7 +117,7 @@ class DictReader: # values while row == []: row = next(self.reader) - d = dict(zip(self.fieldnames, row)) + d = OrderedDict(zip(self.fieldnames, row)) lf = len(self.fieldnames) lr = len(row) if lf < lr: