0

我有一个需要循环的 .csv 文件,然后在循环内,我想在每次迭代时检查下一行的值。我似乎无法让它正常工作,因为它跳过了我在下一次迭代中展望的那一行。

import csv
file_object = open('file.csv', 'r')
reader = csv.reader(file_object, delimiter = ';')

for line in reader:
    next_line = reader.next()
    # It now reads the next line and doesn't iterate over it again in the next
    # iteration. However, i want it to still iterate over it in the next iteration.

非常感谢!

4

1 回答 1

1

您可以“手动”跟踪当前行和下一行:

line = reader.next()
for next_line in reader:
    # Do your processing
    line = next_line
# Now do whatever needs to be done with the very last line
于 2012-11-25T21:48:10.760 回答