I am working on a python project in which I read csv files using pythons csv lib. I dont need all of the files data, just a few lines to do some analysis. So I just want to read in a sample (a certain number of lines). I could simply do that like the following:
num_rows = 1000
with open(path, newline='') as my_file:
sample_reader = csv.reader(my_file)
count = 0
for row in sample_reader:
# do sth with row
count += 1
if count >= num_rows:
break
My problem:
How does 'sample_reader' read in the lines while iterating over it? Does it only read in a 'row' for each for-loop iteration? Or does it use a buffer, or even worse does it read in the whole file before the iteration?
I tried to find an answer reading the doc (https://docs.python.org/3/library/csv.html#csv.reader), and even looked up the code, but I couldnt find any usefull informaiton.
csv.reader.