In python 2.7.3, how can I start the loop from the second row? eg
first_row = cvsreader.next();
for row in ???: #expect to begin the loop from second row
blah...blah...
first_row = next(csvreader) # Compatible with Python 3.x (also 2.7)
for row in csvreader: # begins with second row
# ...
Testing it really works:
>>> import csv
>>> csvreader = csv.reader(['first,second', '2,a', '3,b'])
>>> header = next(csvreader)
>>> for line in csvreader:
print line
['2', 'a']
['3', 'b']
next(reader, None) # Don't raise exception if no line exists
looks most readable IMO
The other alternative is
from itertools import islice
for row in islice(reader, 1, None)
However shouldn't you be using the header? Consider a csv.DictReader
which by default sets the fieldnames to the first line.
Assuming the 1st row contains the field names:
import csv
for field in csv.DictReader(open("./lists/SP500.csv", 'rb')):
symbol = (field['ticker']).rstrip()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.