[Tutor] CSV file processing...

Kent Johnson kent37 at tds.net
Fri Mar 21 00:42:28 CET 2008


Spencer Parker wrote:
> I am trying to read a CSV file and the get that information into a MySQL 
> database.  I am able to do this, but I have a small problem.  I have a 
> piece of software that runs and each iteration is one like.  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.  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.

I'm not sure I understand. It sounds like you have a very long line of 
data from the csv file and you want to split it into groups of 7, after 
taking the first 20 items. If that is correct, something like this might 
work:

row = ... # your long row
prefix = row[:20] # the twenty items that repeat
for i in range(20, len(row), 7):
   next = prefix + row[i:i+7]
   # Handle 'next' - add it to the database or whateve

Kent


More information about the Tutor mailing list