-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
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
Changes from 1 commit
37927b2
9aa3d41
55930fc
48d3ce1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
…ers non-destructive behavior for csv documents with duplicate fieldname values.
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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. | ||
|
@@ -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 | ||
|
@@ -117,7 +117,7 @@ def __next__(self): | |
# values | ||
while row == []: | ||
row = next(self.reader) | ||
d = OrderedDict(zip(self.fieldnames, row)) | ||
d = tuple(zip(self.fieldnames, row)) | ||
lf = len(self.fieldnames) | ||
lr = len(row) | ||
if lf < lr: | ||
|
@@ -128,6 +128,11 @@ def __next__(self): | |
return d | ||
|
||
|
||
class DictReader(TableReader): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There would need to be a DeprecationWarning raised at some point here. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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): | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tests failed due to using tuple instead of list (as line 127 will attempt to write to what is now non-mutable)
Needs to be changed to list(zip...)