AEA*_*AEA 1 python csv match python-2.7
我有大量的csv格式数据,需要根据匹配的两个参数从其中删除行。
我要删除的数据列表如下所示:
London,James Smith
London,John Oliver
London,John-Smith-Harrison
Paris,Hermione
Paris,Trevor Wilson
New York City,Charlie Chaplin
New York City,Ned Stark
New York City,Thoma' Becket
New York City,Ryan-Dover
Run Code Online (Sandbox Code Playgroud)
然后,主csv将根据城市名称与第二列的匹配以及该名称与第9列的名称的匹配来删除行。
如果两者都匹配,请删除主csv中的行(请注意,此处未提供此csv的示例)。
我验证了以下内容是否可以根据您提供/描述的数据类型工作:
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
Run Code Online (Sandbox Code Playgroud)
注:该{... for ... in ...}设置,理解语法; 根据您的Python版本,您可能需要将其更改为等效版本set(... for ... in ...)才能起作用。
| 归档时间: |
|
| 查看次数: |
3728 次 |
| 最近记录: |