Reputation: 4223
How do I skip a line of records in a CSV when using a DictReader?
Code:
import csv
reader = csv.DictReader(open('test2.csv'))
# Skip first line
reader.next()
for row in reader:
print(row)
Error:
Traceback (most recent call last):
File "learn.py", line 3, in <module>
reader.next()
AttributeError: 'DictReader' object has no attribute 'next'
Upvotes: 26
Views: 24240
Reputation: 304375
It was considered a mistake in python2 to have the method called next()
instead of __next__()
next(obj)
now calls obj.__next__()
just like str
, len
etc. as it should.
You usually wouldn't call obj.__next__()
directly just as you wouldn't call obj.__str__()
directly if you wanted the string representation of an object.
Handy to know if you find yourself writing unusual iterators
Upvotes: 6
Reputation: 172309
Since Python 2.6 you should use next(foo) instead of foo.next().
Upvotes: 7
Reputation: 50928
You use next(reader)
instead.
Source: csv.DictReader documentation
Upvotes: 28