[Python-ideas] csv.DictReader could handle headers more intelligently. (original) (raw)

Mark Hackett mark.hackett at metoffice.gov.uk
Fri Jan 25 11:58:28 CET 2013


On Friday 25 Jan 2013, Ethan Furman wrote:

On 01/24/2013 02:47 AM, Mark Hackett wrote: > On Thursday 24 Jan 2013, Steven D'Aprano wrote: >>> I'm not sure this behavior merits the all-caps "AS EXPECTED" label. >>> It's not terribly surprising once you sit down and think about it, but >>> it's certainly at least a little unexpected to me that data is being >>> thrown away with no notice. It's unusual for errors to pass silently >>> in python. >> >> Yes, we should not forget that a CSV file is not a dict. Just because >> DictReader is implemented with a dict as the storage, doesn't mean >> that it should behave exactly like a dict in all things. Multiple >> columns with the same name are legal in CSV, so there should be a reader >> for that situation. > > But just because it's reading a csv file, we shouldn't change how a > dictionary works if you add the same key again.

The proposal is not to change how a dict works, but what the proper response is for DictReader when a duplicate key is found.

Ethan, the proposal is predicated on the "silent abandonment" (which isn't actually the case any more than doing:

a=4 a=9

is abandoning silently the 4.) being unexpected.

Except, just like the assignment in the aside above, this is entirely what IS expected if you're putting a CSV line into a dictionary with duplicate key names.

If you don't want it to do what a dictionary does, then don't use DictReader, as Chris proposes.

My only niggle with that idea is that you'd be writing a lot of "SumptyReader" for each case and is redundant. But that may, in practice, be no problem at all.

If you didn't want it to do what a dict does, don't use a dict.



More information about the Python-ideas mailing list