I have a list of search items:
search = ("EPP3424", "EPP5423", "EPP4567", Continues... )
I want to check each row of a csv file where each row looks like this:
("1206502", "EPP5423", "97334343")
next row...
If any of the items in the search list appear in the row of the csv, add that entire row to a new list.
Problem is I can get it to only match one results, I can’t seem to get it to loop over the items correctly.
csvFile = open(fRoot + "\\SearchEPP.csv", 'r')
try:
csvReader = csv.reader(csvFile)
for row in csvReader:
if all(s in row for s in search):
print "Match"
allEPP.append(row)
else:
print "no match"
finally:
csvFile.close()
Python 2.6, Windows 7
UPDATE:
Here’s what I tried doing based on your response, still only returns one record.
f = open(fRoot + "\\EPP.txt", "r")
search = list()
for row in f:
search.append(row)
search = set(search)
#search = ("EPP2383", "EPP2384")
allEPP = list()
csvFile = open(fRoot + "\\SearchEPP.csv", 'r')
try:
csvReader = csv.reader(csvFile)
for row in csvReader:
if any(r in search for r in row):
print "Match"
allEPP.append(row)
else:
print "."
finally:
csvFile.close()
should probably be:
Note that a better way to do this would be to convert
searchto a set once:And then do your check on the
set(instead of thetuple). Membership tests forsetare typically O(1) whereas they’re O(n) for tuples.Or even:
Although the
anysolution might be faster (depending on how bigrowis and what the overhead of creating a new set via intersection is versus the overhead of a generator expression).As suggested by @RocketDonkey, you probably have newlines in your “search” list which is still causing you problems in your updated code. Here’s one fix: