Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bpo-34003: Use dict instead of OrderedDict in csv.DictReader #8014

Merged
merged 5 commits into from
Jan 31, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions Doc/library/csv.rst
Original file line number Diff line number Diff line change
Expand Up @@ -150,12 +150,12 @@ The :mod:`csv` module defines the following classes:
dialect='excel', *args, **kwds)

Create an object that operates like a regular reader but maps the
information in each row to an :mod:`OrderedDict <collections.OrderedDict>`
whose keys are given by the optional *fieldnames* parameter.
information in each row to a :class:`dict` whose keys are given by the
optional *fieldnames* parameter.

The *fieldnames* parameter is a :term:`sequence`. If *fieldnames* is
omitted, the values in the first row of file *f* will be used as the
fieldnames. Regardless of how the fieldnames are determined, the ordered
fieldnames. Regardless of how the fieldnames are determined, the
dictionary preserves their original ordering.

If a row has more fields than fieldnames, the remaining data is put in a
Expand All @@ -166,8 +166,8 @@ The :mod:`csv` module defines the following classes:
All other optional or keyword arguments are passed to the underlying
:class:`reader` instance.

.. versionchanged:: 3.6
Returned rows are now of type :class:`OrderedDict`.
.. versionchanged:: 3.8
Returned rows are now of type :class:`dict`.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This entry should have been added without deleting the previous one

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like this never got fixed...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR #20657 fixes this


A short usage example::

Expand All @@ -181,7 +181,7 @@ The :mod:`csv` module defines the following classes:
John Cleese

>>> print(row)
OrderedDict([('first_name', 'John'), ('last_name', 'Cleese')])
{'first_name': 'John', 'last_name': 'Cleese'}


.. class:: DictWriter(f, fieldnames, restval='', extrasaction='raise', \
Expand Down
3 changes: 1 addition & 2 deletions Lib/csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
__doc__
from _csv import Dialect as _Dialect

from collections import OrderedDict
from io import StringIO

__all__ = ["QUOTE_MINIMAL", "QUOTE_ALL", "QUOTE_NONNUMERIC", "QUOTE_NONE",
Expand Down Expand Up @@ -117,7 +116,7 @@ def __next__(self):
# values
while row == []:
row = next(self.reader)
d = OrderedDict(zip(self.fieldnames, row))
d = dict(zip(self.fieldnames, row))
lf = len(self.fieldnames)
lr = len(row)
if lf < lr:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
csv.DictReader now creates dicts instead of OrderedDicts. Patch by Michael
Selik.