[Tutor] CSV file processing...

Eric Walstad eric at ericwalstad.com
Fri Mar 21 00:49:45 CET 2008


Hi Spencer
Spencer Parker wrote:
> I have a 
> piece of software that runs and each iteration is one like.
I don't understand what this means.


 >  It only
> runs once right now; there is only one line + the headers.  I use the 
> csv module to kill the headers and import the one line.
Does 'kill the headers' mean the same as ignoring them in your code?


> The problem 
> is...I need to have it split the csv file at some point. I need to first 
> 20 items taken off and then have the next 7 + the first 20 imported into 
> the database...then have it do this for the next 7 + the first 20...so 
> on and so forth until hits the end of the line.
Define 'item' here.  Is it a field, a character, is it a line?
I'm going to assume that you mean that you want to loop through the file 
line by line, inserting to the database the line's fields in this order:
fields[20], ..., fields[27], fields[0], fields[1], ..., fields[n-1]

This example:
http://dpaste.com/hold/40503/

shows how the csv.reader lets you loop over csv data line by line, each 
line is a list of fields.  Using Python's slicing operations you can 
easily rearrange the line's fields to suit your output needs.

See also:
http://www.diveintopython.org/native_data_types/lists.html

Eric.






More information about the Tutor mailing list