Python CSV Reader: No Get Line Function?
This question already has an answer here:
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.
上一篇: Python:产量