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

Mark Hackett mark.hackett at metoffice.gov.uk
Wed Jan 30 13:14:09 CET 2013


On Wednesday 30 Jan 2013, Steven D'Aprano wrote:
> On 30/01/13 21:32, Mark Hackett wrote:
> > If you don't know what's in the csv file at all, then how do you know
> > what you're supposed to do with it.
> 
> Maybe you're processing the file without caring what the column names are,

If you don't care, then you shouldn't be using a dictionary because you have 
to know to say what one you want.

> but you still need to map column name to column contents.

Why? You said this hypothetical reckless person doesn't care.

> This is no more
> unusual than processing a dict where you don't know the keys: you just
>  iterate over them.
> 

Which is only used for printing the info out.

There's a much easier way to do that:

"cat file.csv"

> Or maybe you're scanning the file for one specific column name, and you
>  don't care what the other names are.
> 

Then you'll know if it's duplicated or not.

> Or, most likely, you know what you are *expecting* in the CSV file, but
>  because data files don't always contain what you expect, you want to be
>  notified if there is something unexpected rather than just have it
>  silently do the wrong thing.
> 

There's a way to do that:

"head -n1 file.csv".

You know, have a look.



More information about the Python-ideas mailing list