我验证了以下内容可根据您提供/描述的数据类型的需要进行工作:
import csv
from cStringIO import StringIO
# parse the data you're about to filter with
with open('filters.csv', 'rb') as f:
filters = {(row[0], row[1]) for row in csv.reader(f, delimiter=',')}
out_f = StringIO() # use e.g. `with open('out.csv', 'wb') as out_f` for real file output
out = csv.writer(out_f, delimiter=',')
# go thru your rows and see if the pair (row[1], row[8]) is
# found in the previously parsed set of filters; if yes, skip the row
with open('data.csv', 'rb') as f:
for row in csv.reader(f, delimiter=','):
if (row[1], row[8]) not in filters:
out.writerow(row)
# for debugging only
print out_f.getvalue() # prints the resulting filtered CSV data
注意: is {... for ... in ...}
set-comprehension 语法;根据您的 Python 版本,您可能需要将其更改为等效版本set(... for ... in ...)
才能正常工作。