High level csv reader

skip at pobox.com skip at pobox.com
Wed Nov 1 12:12:05 CET 2006

    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

    * 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.


More information about the Python-list mailing list