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

Stephen J. Turnbull stephen at xemacs.org
Tue Jan 29 09:17:46 CET 2013


Shane Green writes:

 > Actually I've seen a many real life examples of CSV files with
 > repeated column names,

Sure, but this really isn't the issue.  If it were, "cvs.reader is
your friend" would be all the answer that the issue deserves IMHO.

 > It seems like we're getting too hung up on dicts:

Not at all.  (For reasons I don't understand) Somebody has a use case
where it's useful to have the field names stored in each record,
rather than stored once and have both field names and field values
accessed by position as needed.  The point is to return a name-value
*mapping object* for *each* row, and that may as well be a dict.

The people who suggest a multidict or a list-valued dict are missing
that point, AFAICS.  Eg, in your "BLABLA", "VALUE", ..., "VALUE"
example, position really is what matters, so a dict of any kind is
inappropriate IMO.  Again, it's arbitrary whether the list-valued dict
does d["VALUE"].append(x) or d["VALUE"].insert(0,x), and it's hard for
me to guess which it would do in practice: .append is easier to write,
but .insert seems closer to the behavior of csv.reader (which is what
we really want in your example IMO).




More information about the Python-ideas mailing list