Python 3.2 skip a line in csv.DictReader

前端 未结 3 1359
说谎
说谎 2020-12-20 11:26

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
rea         


        
相关标签:
3条回答
  • 2020-12-20 11:36

    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

    0 讨论(0)
  • 2020-12-20 11:39

    You use next(reader) instead.

    Source: csv.DictReader documentation

    0 讨论(0)
  • 2020-12-20 11:53

    Since Python 2.6 you should use next(foo) instead of foo.next().

    0 讨论(0)
提交回复
热议问题