[Python-ideas] csv.DictReader could handle headers more intelligently.
Stephen J. Turnbull
stephen at xemacs.org
Sat Jan 26 14:53:53 CET 2013
Shane Green writes:
> And while it's true that a dictionary is a dictionary and it works
> the way it works, the real point that drives home is that it's an
> inappropriate mechanism for dealing ordered rows of sequential
> values.
Right! So use csv.reader, or csv.DictReader with an explicit
fieldnames argument.
The point of csv.DictReader with default fieldnames is to take a
"well-behaved" table and turn it into a sequence of "poor-man's"
objects.
> The final point is a simple one: while that CSV file format was
> stupid, it was perfectly legal. Something that deals with CSV
> content should not be losing any of its content.
That's a reasonable requirement.
> It also should [not] be barfing or throwing exceptions, by the way.
That's not. As long as the module provides classes capable of
handling any CSV format (it does), it may also provide convenience
classes for special purposes with restricted formats. Those classes
may throw exceptions on input that doesn't satisfy the restrictions.
> And what about fixing it by replacing implementing a class that
> does it correctly, [...]?
Doesn't help users who want automatically detected access-by-name.
They must have unique field names. (I don't have a use case. I
assume the implementer of csv.DictReader did.<wink/>)
More information about the Python-ideas
mailing list