Can only iterate once through csv reader

Question:

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 strings, and for each one, loop through the rows of the CSV file checking each string in the first column of the CSV to see if it occurs in my string, and if it does, add the number in the other column to something. A minimal sort of example would be

import csv
sList = ['a cat', 'great wall', 'mediocre wall']
vals = []
with open('file.csv', 'r') as f:
    r = csv.reader(f)
    for w in sList:
        val = 0
        for row in r:
            if row[0] in w:
                val += 1
        vals.append(val)

An example of a CSV file with which I might use this could be

a, 1
great, 2

Of course csv.reader(f) creates an iterable that I can loop through only once. I’ve seen recommendations elsewhere to use itertools but all of the recommendations I’ve found have been for problems that involve looping through the CSV file a small number of times, usually just twice. If I tried to use this to loop through the CSV many times I’m unsure of what that would mean for memory consumption, and in general I’m just wondering about the smartest way to approach this problem.

Asked By: Addem

||

Answers:

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)
Answered By: Marcin
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.