8

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.

Addem
  • 3,635
  • 3
  • 35
  • 58
  • How big is the file? Can you just read the entire thing into a dictionary and perform your lookups against the resulting dictionary? – Larry Lustig Dec 08 '14 at 03:33

1 Answers1

9

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)
Marcin
  • 215,873
  • 14
  • 235
  • 294
  • Interesting, I'll try this--but are you sure I set f.seek(0) rather than r.seek(0)? Just checking, thank you for the help! – Addem Dec 08 '14 at 03:40
  • 1
    @Addem. Im sure, I tested this before posting on python 3.4. – Marcin Dec 08 '14 at 03:42