Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read only a specific range of lines out of a csv file with python?

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.

like image 408
Eternal_Sunshine Avatar asked Apr 16 '15 12:04

Eternal_Sunshine


1 Answers

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])
like image 160
Mazdak Avatar answered Sep 30 '22 05:09

Mazdak