1

I have to read a CSV file N lines at a time.

csv_reader = csv.reader(csv_file, delimiter=',')
    line_count = 0
    for row in csv_reader:
        print row

I know I can loop N times at a time, build a list of list and process that way.

But is there a simpler way of using csv_reader so that I read n lines at a time.

AlexisG
  • 2,476
  • 3
  • 11
  • 25
Exploring
  • 2,493
  • 11
  • 56
  • 97

2 Answers2

1

Hi I don't think that you'll be able to do that without a loop with csv package.

You should use pandas (pip install --user pandas) instead:

import pandas

df = pandas.read_csv('myfile.csv')

start = 0
step = 2 # Your 'N'

for i in range(0, len(df), step):
    print(df[i:i+step])
    start = i
Félix Herbinet
  • 257
  • 1
  • 8
0

Pandas has a chunksize option to their read_csv() method and I would probably explore that option.

If I was going to do it myself by hand, I would probably do something like:

import csv

def process_batch(rows):
    print(rows)

def get_batch(reader, batch_size):
    return [row for _ in range(batch_size) if (row:=next(reader, None))]

with open("data.csv", "r") as file_in:
    reader = csv.reader(file_in)
    while batch := get_batch(reader, 5):
        process_batch(batch)
JonSG
  • 10,542
  • 2
  • 25
  • 36