Python CSV Reader: No Get Line Function?

This question already has an answer here:

  • Accessing the index in 'for' loops? 17 answers

  • The reader object is an iterator, so you can always use enumerate to get the line number:

    reader = csv.DictReader(readFile)
    for line_number, row in enumerate(reader):
         # some code
    

    enumerate also lets you specify a custom starting index:

    for line_number, row in enumerate(reader, 2):
        # line number starts at 2
    

    enumerate isn't only limited to the reader object of the csv module but works in general with iterables and iterators.

    链接地址: http://www.djcxy.com/p/24024.html

    上一篇: Python:产量

    下一篇: Python CSV阅读器:没有获得线路功能?