8000 bpo-29614: Rename and reimplement csv.DictReader. by foxfluff · Pull Request #223 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

bpo-29614: Rename and reimplement csv.DictReader. #223

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

Closed
wants to merge 4 commits into from
Closed
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
17 changes: 11 additions & 6 deletions Lib/csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
"Error", "Dialect", "__doc__", "excel", "excel_tab",
"field_size_limit", "reader", "writer",
"register_dialect", "get_dialect", "list_dialects", "Sniffer",
"unregister_dialect", "__version__", "DictReader", "DictWriter",
"unix_dialect"]
"unregister_dialect", "__version__", "TableReader", "DictReader",
"DictWriter", "unix_dialect"]

class Dialect:
"""Describe a CSV dialect.
Expand Down Expand Up @@ -78,7 +78,7 @@ class unix_dialect(Dialect):
register_dialect("unix", unix_dialect)


class DictReader:
class TableReader:
def __init__(self, f, fieldnames=None, restkey=None, restval=None,
dialect="excel", *args, **kwds):
self._fieldnames = fieldnames # list of keys for the dict
Expand Down Expand Up @@ -117,17 +117,22 @@ def __next__(self):
# values
while row == []:
row = next(self.reader)
d = OrderedDict(zip(self.fieldnames, row))
d = list(zip(self.fieldnames, row))
lf = len(self.fieldnames)
lr = len(row)
if lf < lr:
d[self.restkey] = row[lf:]
d.append((self.restkey, row[lf:]))
elif lf > lr:
for key in self.fieldnames[lr:]:
d[key] = self.restval
d.append((key, self.restval))
return d


class DictReader(TableReader):
Copy link
Member

Choose a reason for hiding this comment

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

There would need to be a DeprecationWarning raised at some point here.

Copy link
Author

Choose a reason for hiding this comment

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

Would a DeprecationWarning be appropriate here? as this ideally would not affect the behavior of DictReader, and would instead just add the "new" functionality of TableReader.

def __next__(self):
return OrderedDict(super().__next__())


class DictWriter:
def __init__(self, f, fieldnames, restval="", extrasaction="raise",
dialect="excel", *args, **kwds):
Expand Down
0