[Python-ideas] csv.DictReader could handle headers more intelligently.

Mark Hackett mark.hackett at metoffice.gov.uk
Thu Jan 24 11:47:17 CET 2013


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.

Duplicate headings in a csv file are as legal as using the same name for 
something else in a programming language.

e.g.

endvalue=a+b+c/5
...code using that result...
endvalue = os.printerr(file_descriptor)
...print out an error string...

this is "legal" but really REALLY smelly.

Similarly a multivalued csv file.

Excel uses the column ID not the name on the first row, to identify the columns 
in its macro language. Because otherwise which "endvalue" column did you mean?



More information about the Python-ideas mailing list