我有一个搜索项目列表:
search = ("EPP3424", "EPP5423", "EPP4567", Continues... )
我想检查 csv 文件的每一行,其中每一行如下所示:
("1206502", "EPP5423", "97334343")
next row...
如果搜索列表中的任何项目出现在 csv 行中,则将该整行添加到新列表中。
问题是我可以让它只匹配一个结果,我似乎无法让它正确地循环这些项目。
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
更新:
这是我根据您的回复尝试做的事情,仍然只返回一条记录。
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()