apettey apettey - 5 months ago 23
Python Question

Python CSV search

I am fairly new to python and have run into an issue which I believe to be strange. I am searching through the first column of a csvfile.

I am using the csv module and have some code with nested for loops. My intentions were for the loop in the middle to restart from the first row in the csvfile every time it finds a match. But instead it always starts from the last row being searched in the csv.

My code below and the results will make my problem more apparent.

number = [1,3,5,6,7,8,1234,324,5,2,35]

import csv

with open('...../Documents/pycharm/testcsv.csv', 'rb') as csvimport:
csvfile = csv.reader(csvimport)

for num in number:
print 'looking for ' + str(num)
is_in_file = False
print 'set to false'
for row in csvfile:
print 'looking at value ' + row[0]
if row[0] == str(num):
is_in_file = True
print 'match, set to true'
break
print 'test1'
if is_in_file == False:
print str(num) + ' not found in file!'


Here is what gets printed in the IDE:

looking for 1
set to false
looking at value a
looking at value 1
match, set to true
test1
looking for 3
set to false


NOTE: here I wish to look at the first line of the csvfile (value a). Instead it looks at the third line of the csvfile (value '').

looking at value
looking at value 1234
looking at value 7
looking at value 1
looking at value 3
match, set to true
test1


From here on out it skips my inner for loop all together as it has gone through the last row:

looking for 5
set to false
looking at value 5
match, set to true
test1
looking for 6
set to false
looking at value 6
match, set to true
test1
looking for 7
set to false
looking at value 77
looking at value 23
looking at value 87
test1
7 not found in file!
looking for 8
set to false
test1
8 not found in file!
looking for 1234
set to false
test1
1234 not found in file!
looking for 324
set to false
test1
324 not found in file!
looking for 5
set to false
test1
5 not found in file!
looking for 2
set to false
test1
2 not found in file!
looking for 35
set to false
test1
35 not found in file!


Here is the csvfile

a,c,b,d,e
1,3,4,5,6
,7,7,,87
1234,1,98,7,76
7,8,90,0,8
1,3,98,0,0
3,cat,food,20,39
5,%,3,6,90
6,2,2,2,3
77,3,4,3,5
23,3,4,3,6
87,5,5,5,

Answer

csvfile is a generator: first time it will work, but second time it will return immediately. Consider doing this:

csvfile = list(csv.reader(csvimport))

Then you can scan csvfile as much as you want.

BUT, this code is not really performant as you perform a linear scan on your file. Consider doing a dictionary instead. Here's how

d = dict()
for r in csvfile:
    d[r[0]] = r[1:]

Then, replace your inner loop by:

if num in d:
   is_in_file = True
   print("%d is in file" % num)