Skip to content Skip to sidebar Skip to footer

Can Only Iterate Once Through Csv Reader

So I basically have an extremely long list of strings, and a CSV file that contains a column of strings and a column of numbers. I need to loop through the extremely long list of

Solution 1:

You need to "reset" the file iterator:

import csv
sList = ['a cat', 'great wall', 'mediocre wall']
vals = []
with open('data.csv', 'r') as f:
    r = csv.reader(f)
    for w in sList:
        val = 0
        f.seek(0)  #<-- set the iterator to beginning of the input file
        for row in r:
            print(row)
            if row[0] in w:
                val += 1
        vals.append(val)

Post a Comment for "Can Only Iterate Once Through Csv Reader"