I am trying to compare two csv files and write to a third if they match (The comparison/writing works fine). My issue is with iteration. In the below example, my program will print 1 as many times as there are lines in s. If i move the print up a level and remove the for loop for s, it prints as many lines as there are in z. I essentially want it toi compare every row in z against every row in s but it only does it for the first row in z and exits
import csv
if __name__ == "__main__":
with open("z_file.csv", "r") as f:
with open("s_file.csv", "r") as g:
with open("results.csv", "a+", newline="") as h:
z = csv.reader(f)
s = csv.reader(g)
final = csv.writer(h)
i = 0
for zrow in z:
i += 1
for srow in s:
print(i)
s
the first time it is done. You can’t loop through an iterator twice. You’ll need to save it to a proper list.with open("z_file.csv", "r") as f, open("s_file.csv", "r") as g:
to save yourself an indent level