简体   繁体   中英

Compare a searchlist to each row of csv file using Python

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()
if all(s in row for s in search):

should probably be:

if any(s in row for s in search):

Note that a better way to do this would be to convert search to a set once:

search = set(search)
...

And then do your check on the set (instead of the tuple ). Membership tests for set are typically O(1) whereas they're O(n) for tuples.

if any(r in search for r in row):

Or even:

if search.intersection(row):
   ...

Although the any solution might be faster (depending on how big row is 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:

with open(fRoot + "\\EPP.txt", "r") as f:
    search = set(line.rstrip('\n') for line in f)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM