Skip to content
Advertisement

For loops won’t nest

I’m writing a script to compare two csv files. The plan is for the script to find items with the same ID and send them to a third csv.

I’m using a nested for loop for this. It should pick a line in the first file and then compare it to each line (one at a time) in the second. So far so good.

The script should repeat this for every line in the first file. However, after some debugging I found that the script only compares the first line with each line in the second before stopping. The other lines are never compared.

I can’t see why for the life of me. I’m already cringing in case this turns out to be a schoolboy error but thought it would be worth asking the community.

Any ideas?

outdata = []
newPrices = open(csv1)
oldPrices = open (csv2)
priceComparison = open (csv3)

new=csv.reader(newPrices, delimiter=',')
old=csv.reader(oldPrices, delimiter=',')
writer=csv.writer(priceComparison,delimiter=',')
for row1 in new:
    print(row1[0])
    for row2 in old:
        print(row1[0]) #Seems to be sticking on the first value and not looping the first for loop
        if row1[0].upper == row2[0]:
            print("row: ", row)
            outdata.append([row1])
            outdata.append([row2])
        
print(outdata)
writer.writerows(outdata)

Advertisement

Answer

I’m not 100% sure, but my guess is that csv.reader returns an iterator, which can only be consumed once. Hence why you only loop through old once. Try putting the lines in lists before traversing them, like this:

new = list(csv.reader(newPrices, delimiter=","))
old = list(csv.reader(oldPrices, delimiter=","))

Here’s a link to the documentation in case it’s helpful: https://docs.python.org/3/library/csv.html#csv.reader

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement