0

I am trying to read the subset of some data from a large CSV file, I have tried using nrows, but it returns the following error message, 'TypeError: 'nrows' is an invalid keyword argument for this function' unsure how to correct it.

with open('file.csv') as csv_file:
    csv_reader = reader(csv_file, nrows=350)
    lines = list(csv_reader)
    print(lines) 

#Aim was to print 350 rows in a list of lists

TypeError: 'nrows' is an invalid keyword argument for this function
ShaqSD
  • 49
  • 3

1 Answers1

0

You can use for example itertools.islice to read only 350 lines from the csv_reader:

from csv import reader
from itertools import islice

with open('file.csv', 'r') as csv_file:
    csv_reader = reader(csv_file)
    lines = list(islice(csv_reader, 0, 350))
    print(lines)
Andrej Kesely
  • 168,389
  • 15
  • 48
  • 91