10

How to loop through a specific range of rows with Python csv reader?

The following code loops through all rows:

with open(trainFile, 'rt') as csvfile:
    spamreader = csv.reader(csvfile, delimiter=' ', quotechar='|')
    for row in spamreader:
        print (', '.join(row))

I wish to loop only from given (i to j).

3 Answers 3

10

You can use itertools.islice:

import itertools

i, j = 10, 20
with open(trainFile, 'rt') as csvfile:
    spamreader = csv.reader(csvfile, delimiter=' ', quotechar='|')
    for row in itertools.islice(spamreader, i, j+1):
        print (', '.join(row))

Alternative (following code is possible because csv.reader accept an iterable):

NOTE: only works when CSV rows do not contain newline.

import itertools

i, j = 10, 20
with open(trainFile, 'rt') as csvfile:
    spamreader = csv.reader(itertools.islice(csvfile, i, j+1),
                            delimiter=' ', quotechar='|')
    for row in spamreader:
        print (', '.join(row))
Sign up to request clarification or add additional context in comments.

3 Comments

@kworr, Thank you for comment. I mention that.
In your alternative the values of i and j aren't referenced...so I doubt it works as shown.
@martineau, Thank you for pointing it out. I updated the alternative accordingly.
2

Use islice, eg:

rows_1_to_50 = itertools.islice(spamreader, 0, 50)
for row in rows_1_to_50:
    pass

Comments

2

Another itertools implementation using dropwhile and takewhile

from itertools import takewhile, dropwhile
trainFile = 'x.1'
low_lim = 3
high_lim = 6
with open(trainFile, 'rt') as csvfile:
    spamreader = csv.reader(csvfile, delimiter=' ', quotechar='|')
    g = (x for x in enumerate(spamreader, 1))
    g = dropwhile(lambda x: x[0] < low_lim, g)
    g = takewhile(lambda x: x[0] <= high_lim, g)
    for row in g:
        print (', '.join(row[1]))

2 Comments

When you say trainFile = 'x.1' that refers to the complete path, correct?
@MattO'Brien, that's correct. Could be complete or relative

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.