High level csv reader
sjmachin at lexicon.net
Wed Nov 1 11:17:55 CET 2006
George Sakkis wrote:
> It occured to me that most times I read a csv file, I'm often doing
> from scratch things like assigning labels to columns, mapping fields to
> the appropriate type, ignoring some fields, changing their order, etc.
> Before I go on and reinvent the wheel, is there a generic high level
> wrapper around csv.reader that does all this ?
Firstly one thing to remember: if you are going to reinvent the wheel,
don't forget to also reinvent the axle :-)
AFAIK there is no such animal.
I would need a lot of persuasion that a wrapper or toolbox wasn't just
overhead with little benefit. For example, ignoring some fields and/or
changing the order of others can be done rather simply:
| >>> inrow
| ['a', 'b', 'c', 'd', 'e', 'f']
Suppose we want to ignore the "vowels" and reverse the order of the
| >>> outrow = list(inrow[k] for k in (5, 3, 2, 1))
| >>> outrow
| ['f', 'd', 'c', 'b']
I don't see the value in creating (and documenting!) a one-line
def gather_list_items(input_list, item_map):
return list(input_list[k] for k in item_map)
NB something like this is already in the mx kit somewhere IIRC.
More information about the Python-list