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
Post a Comment