csv中的python查找

时间:2016-06-27 08:32:38

标签: python csv ip-address lookup

Child Block,Container
10.79.8.0/24,AT
10.80.0.64/26,DE
10.80.0.128/26,IT
10.80.0.192/26,DE
10.80.1.0/26,AT
10.80.1.64/26,FR
10.80.1.128/26,IT
10.80.1.192/26,AT
10.80.2.0/26,DE
10.80.2.64/26,FR
10.80.2.128/26,IT
10.80.2.192/26,AT
10.80.3.0/26,DE
10.80.3.64/26,AT
10.80.3.128/26,FR

输入是2个ip地址(例如:10.80.2.55和10.80.2.45),该代码在上面的列表中查找,如果有匹配则返回容器字符串(例如:DE FR)。 但是我的代码只返回一个字符串DE或FR。我应该如何隔离“for”循环以执行查找和返回两个字符串?

import csv
import sys
from netaddr import IPNetwork, IPAddress

try:
f = open('C:\\Users\\609593705\\test.csv', 'rt')
reader = csv.DictReader(f)
for row in reader:
    if IPAddress(sys.argv[1]) in IPNetwork(row['Child Block']):
        print (row['Container'])
        for row2 in reader:         
            if IPAddress(sys.argv[2]) in IPNetwork(row2['Child   Block']):        
                print (row2['Container'])

finally:
f.close()

0 个答案:

没有答案