5

How can I only read from line 5000 to 6000 in this csv file for example? At this moment "for row in reader:" loops through all lines of course.

So I have the lines:

with open('A.csv', 'rt') as f:
     reader = csv.reader(f, delimiter=';')
     for row in reader:
           response = urllib2.urlopen(row[12])

This code is used to open specific url links.

0

2 Answers 2

9

Because csv reader object supports iteration, you can simply use itertools.islice to slice any specific part that you want.

from itertools import islice

with open('A.csv', 'rt') as f:
     reader = csv.reader(f, delimiter=';')
     for row in islice(reader,5000,6000):
           response = urllib2.urlopen(row[12])
Sign up to request clarification or add additional context in comments.

Comments

2

You can use a generator in your for loop with enumerate:

with open('A.csv', 'rt') as f:
     reader = csv.reader(f, delimiter=';')
     for row in (r for i, r in enumerate(reader) if 5000<=i<=6000):
           response = urllib2.urlopen(row[12])

Since csv.reader, enumerate and the generator expression itself are all generators, you will be only dealing with one at a time.

Comments

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.