简体   繁体   English

正在读取.csv文件。

[英]reading .csv file.

I have a .csv file i need to loop over, and then inside a loop, i want to check the value of the next line every iteration. 我需要循环播放一个.csv文件,然后在循环中,我想在每次迭代中检查下一行的值。 I can't seem to get it to work properly, as it skips the line i was looking ahead in the next iteration. 我似乎无法使其正常工作,因为它跳过了我在下一次迭代中展望的目标。

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.

Thank you very much! 非常感谢你!

You could keep track of both the current and the next line "manually:" 您可以“手动”跟踪当前行和下一行:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM