Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Only read certain rows in a csv file with python

Tags:

python

csv

I want to read only a certain amount of rows starting from a certain row in a csv file without iterating over the whole csv file to reach this certain point.

Lets say i have a csv file with 100 rows and i want to read only row 50 to 60. I dont want to iterate from row 1 to 49 to reach row 50 to start reading. Can i somehow achieve this with seek()?

For example: Seek to row 50 read from 50 to 60

next time: seek to row 27 read 27 to 34 and so on

So not only seeking continuesly forward through the file but also backwards.

Thank you a lot

like image 351
Horst Avatar asked Jul 12 '26 00:07

Horst


1 Answers

An option would be to use Pandas. For example:

import pandas as pd
# Select file 
infile = r'path/file'
# Use skiprows to choose starting point and nrows to choose number of rows
data = pd.read_csv(infile, skiprows = 50, nrows=10)
like image 127
Dylan_w Avatar answered Jul 14 '26 13:07

Dylan_w