2 回答
TA贡献1784条经验 获得超7个赞
根据 Niels 的说法,我会更改 2 循环并遍历行本身并检查当前行条目是否在“坏”列表中:
for row in filereader:
for s in row:
if s not in stringlist:
data1 = row[0]
而且我也不知道您想对 data1 做什么,但是当项目不在 stringList 中时,您总是会更改对象引用。您可以使用列表将项目添加到列表中data1.append(item)
TA贡献1776条经验 获得超12个赞
你可以尝试这样的事情。
stringList = [] #strings look like "AAA", "AAB", "AAC", etc.
with open('BadStrings.csv', 'r')as csvfile:
filereader = csv.reader(csvfile, delimiter=',')
for row in filereader:
stringToExclude = row[0]
stringList.append(stringToExclude)
data1 = [] # Right now you are overwriting your data1 every time. I don't know what you want to do with it, but you could for exmaple add all row[1] to a list data1
with open('OtherData.csv', 'r')as csvfile:
filereader = csv.reader(csvfile, delimiter=',')
next(filereader, None) #Skip header row
for row in filereader:
found_s = False
for s in stringList:
if s in row:
found_s = True
break
if not found_s:
data1.append(row[1]) # Add row[1] to the list is no element of stringList is found in row.
仍然可能不是一个巨大的性能改进,但至少 for 循环for s in stringList:现在会在找到 s 后停止。
添加回答
举报