High level csv reader

George Sakkis george.sakkis at gmail.com
Wed Nov 1 09:33:52 EST 2006


skip at pobox.com wrote:

> George> It occured to me that most times I read a csv file, I'm often
>     George> doing from scratch things like assigning labels to columns,
>     George> mapping fields to the appropriate type, ignoring some fields,
>     George> changing their order, etc.  Before I go on and reinvent the
>     George> wheel, is there a generic high level wrapper around csv.reader
>     George> that does all this ?
>
> I'm not aware of anything that specifically addresses these ideas.  Here are
> some thoughts though:
>
>     * If you use the DictReader class you can ignore fields you aren't
>       interested in more easily since you access the fields of interest by
>       name and with the fieldnames parameter to the constructor can assign
>       column names to csv data which lacks it (use similar functionality in
>       DictWriter to create column labels on output).  You can also specify
>       the restkey parameter to the constructor and thus only specify the
>       fields of interest in the fieldnames parameter.  (I think.  I've never
>       actually used that capability, but that's what the documentation
>       suggests.)
>
>     * There was a thread earlier this month with this subject:
>
>         paseline(my favorite simple script): does something similar exist?
>
>       Check it out for a number of different solutions to formatting the
>       fields in a line of text.  The idea can easily be extended to a list
>       or dict of values instead, perhaps in a subclass of DictReader.
>
> Skip

Indeed, they are both relevant; actually Fredrik's suggestion in that
thread was my starting point. Here's my my current API for the (most
typical) case of fixed-size rows, addressing my most common
requirement, field conversions (no named columns for now):
http://rafb.net/paste/results/4UgvSD50.html.

It looks somewhat involved but it's more along the lines of "making
easy things easy and hard things possible". Comments and suggestions
are most welcome.

George




More information about the Python-list mailing list