简体   繁体   中英

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. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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