[Tutor] Execute on 200 line segments of CSV

Peter Otten __peter__ at web.de
Fri Apr 10 09:57:25 CEST 2015


Kale Good wrote:

> Python newbie here; I'm trying to figure out how to get python to
> execute code on 200-line chunks of a 5000 line CSV. I haven't a clue
> where to start on this. So the code would execute on lines 1-200,
> 201-400, etc.

Use the csv module 

https://docs.python.org/3.4/library/csv.html

to read the rows from the CSV.

Iterate over the rows with a for loop and collect the rows in a list (let's 
call it 'chunk').
Check the chunk list's length on every iteration of the for loop. When the 
list's length is 200 do what you want with those 200 rows, then replace the 
list with a new empty list.

When the for loop ends there may be lines remaining in the chunk list, e. g. 
when the CSV file has 5100 lines there will be a final chunk containing 100 
rows that is not handled by the code in the for loop. It's up you to detect 
it and to decide what to do with this smaller chunk.

Come back when you have some code that we can help fix or improve.



More information about the Tutor mailing list