python batch reading of csv file -


i'm trying batch reading of csv file , process batch callback.

import csv  open('file.csv', 'r') csvfile:     reader = csv.reader(csvfile)     header = next(reader) # skip header      batch_size = 3     batch = []     count = 0      row in reader:         if count >= batch_size:             do_something(batch)             batch = []             count = 0          batch.append(row)         count += 1 

let's assume csv file has 10 rows (without header), , batch_size 3. expected result should 4 batches. 3 batches 3 rows , 4-th batch contain 1 row. code wrote produces 3 batches. if batch size 1/2/5/10 -- ok.

your condition count >= batch_size not become true last few rows in case number of rows cannot divided batch_size without producing remainder.

therefore, need manually clear last batch / remainder. append after loop:

if batch:     do_something(batch) 

this call function again, in case last few rows have been accumulated batch (which loop does, iterates on rows available).


Comments

Popular posts from this blog

Retrieving ETA (estimated time of arrival) with Google Distance Matrix API and public transit as transport mode -

android - ConstraintLayout: Realign baseline constraint in case if dependent view visibility was set to GONE -

c# - Populating Gridview inside Listview ItemTemplate On Web User Control from Code Behind -