I am trying to compare data from columns from two different files. I’ve attempted to use a for, and now a list comprehension.
The issue is that the outer for loop is not being iterated through, but the inner one is. I’ve checked individually and iteration is just fine; but once I nest I get this issue. Is there something I missing with this?
import csv
newInv = csv.reader(open("new.csv", "r"))
origInv = csv.reader(open("old.csv", "r"))
print [ oldrow[5] + " " + newrow[3] for oldrow in origInv for newrow in newInv ]
The outer is iterated. However, the inner loop is only executed for the first iteration of the outer loop – then the end of newInv has been reached.
Understand that
newInvis not a set that you can iterate over multiple times. It is an iterator that you can use only once. Try this (untested):which will copy the data into memory and allow you to iterate over multiple times.