我有一个CSV列出了A列和B列中的IP地址。我想查看A列中的任何IP是否在B列中,如果是,则将整行写入新CSV。它现在的工作方式是将它自己与旁边的行进行比较,而不是A 1检查B 1,2,3,4 ...行。 B列中的IP地址比A多得多,当前的脚本也会对它们进行检查,但它们不应该是空白的(下面提到的解决方法并不起作用)。
import csv
f = open('matching.csv', 'wb') # Final file
writer = csv.writer(f)
with open('input', 'rb') as csvfile:
spreadsheet = csv.reader(csvfile)
for ip in spreadsheet:
if ip[0] in ip[1]: #If the IPs in Column A are in Column B
#if ip[0] in ip[1] and ip[0] != "": # This makes a blank file
try:
writer.writerow(ip[1:]) # Matched, write matching IP row
except:
print "Issue writing to new file", ip[0:2]
else:
print "Col A IP not in Col B ", ip[0:2]
示例(Col A,Col B,Col C ...):
10.11.11.11, 10.22.22.22, foo1
10.22.22.25,10.33.33.33, foo2
10.33.33.33, 10.44.44.44, foo3
匹配输出:10.33.33.33, foo2
答案 0 :(得分:3)
我会创建A列值set
,然后使用in
运算符测试每个B列值。
import csv
# TESTED with Python2
with open('input', 'rb') as csvfile:
spreadsheet = list(csv.reader(csvfile))
column_a = set(line[0] for line in spreadsheet)
matches = [line[1:] for line in spreadsheet if line[1] in column_a]
with open('match.csv', 'wb') as f:
writer = csv.writer(f)
writer.writerows(matches)
鉴于此输入:
2,1,George
4,2,John
6,3,Tom
8,4,Jim1
10,5,Jim2
,6,JohnQ
,7,Andy
,8,Marty
,9,Bill
脚本将2,4,6和8标识为A列中的条目,每个条目与B列中的某些条目匹配。然后将相应的列B-Z值写入新的csv中。
结果如下:
2,John
4,Jim1
6,JohnQ
8,Marty