In python 2.7.3, how can I start the loop from the second row? e.g.
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() was removed in Python 3. I'd use next(csvreader).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.
enumerate(cvsreader) ... if index > 0: .... :)