Here requirement is that, I want to process a file from row number N to row number N+X

One solution might be, use itertools.islice to get the lines from the file between the given range of start and end.
e.g for row no 3 to 6 :

$ python3
Python 3.8.5 (default, Jan 27 2021, 15:41:15) 
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import itertools
>>> def readFile(inputFilePath, start, end) :
...   with open(inputFilePath, 'r', encoding='utf-8') as readFrom:
...     lines = itertools.islice(readFrom, start, end)
...     for line in lines :
...       print (line)
... 
>>> readFile('inputfile.csv', 2, 6);
2, val1, val2, val3, val4

3, val1, val2, val3, val4

4, val1, val2, val3, val4

5, val1, val2, val3, val4

For example purpose I have take a file with few records,

$ cat inputfile.csv
lineno, col1, col2, col3, col4
1, val1, val2, val3, val4
2, val1, val2, val3, val4
3, val1, val2, val3, val4
4, val1, val2, val3, val4
5, val1, val2, val3, val4
6, val1, val2, val3, val4
7, val1, val2, val3, val4
8, val1, val2, val3, val4
9, val1, val2, val3, val4
10, val1, val2, val3, val4
11, val1, val2, val3, val4
12, val1, val2, val3, val4
13, val1, val2, val3, val4
14, val1, val2, val3, val4
15, val1, val2, val3, val4
16, val1, val2, val3, val4
17, val1, val2, val3, val4
18, val1, val2, val3, val4
19, val1, val2, val3, val4
20, val1, val2, val3, val4

Leave a Reply

Your email address will not be published. Required fields are marked *