[Tutor] Processing rows from CSV

Joe Bennett jammer10000 at gmail.com
Sat Nov 21 17:09:30 CET 2009


Doing this a lot in my daily activities... your example

> For row in reader:
>    If row['foo'] == 'something' :
>        do this etc.

 'row' will return a row within 'reader'... You can then pick through
the headers like "foo" and see what is there... I have some compare
scripts that do exactly what you are doing... Iterate by row, then by
column comparing the data in each field. They mostly compare
everything as a string. If I need to float something in a known data
set, I just specifically pull that data out and change it... There are
probably more efficient ways of doing it, but it only takes the script
about 15 seconds to do its thing, so what is saving another second or
two? :)





On Tue, Nov 17, 2009 at 12:57 PM,  <mhw at doctors.net.uk> wrote:
> Dear Tutors,
>
> A rather general question, I'm afraid. I have found myself writing some python code to handle some CSV data, using the csv. DictReader that generates a dict for each row with the key as the column heading and the value in the file as the item. Most operations involve code of the form: (Apologies for incorrect caps)
>
> For row in reader:
>    If row['foo'] == 'something' :
>        do this etc.
>
> Typically I'm checking some form of consistency, or adding an element to the row based on something in the row.
>
> I know about the existence of awk/ sed etc. Which could do some of this, but I ran into some problems with date manipulation, etc that they don't seem to handle very well.
>
> I wanted to ask if anyone knew of anything similar, as I'm worried about re-inventing the wheel. One option would be to use (e.g.) sqlite and then use select/ insert commands, but personally I'd much rather write conditions in python than sql.
>
> My other question was about how I do this more efficiently: I don't want to read the whole thing into memory at once, but (AFAIK) csv.DictReader doesn't support something like readline() to deliver it one at a time.
>
> Any comments gratefully received.
>
> Matt
> Sent from my BlackBerry® wireless device
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>


More information about the Tutor mailing list