excel표 A의 ip가 다른 excel표 B의 ip에 있는지 판단하기;

 :SereneMA
 :https://www.nowcoder.com/discuss/286013import IPy 
import pandas as pd
 
# no header
data = pd.read_excel('xxx/company_ip.xlsx',header=None)
# have header
#data = pd.read_excel('xxx/white_black.xlsx')
 
#1.Read the excel as row, separated by "," or ","
data_list = []
for i in range(len(data)):
    li = str(data.iloc[i].tolist()[0])
    lis = li.split(',')
    l = lis[0].split(',')
    for j in range(len(l)):
        data_list.append(l[j])
 
# print(data_list)
 
# Use ipy.ip () to get all ip
all_white_black = []
specail_ip_range=[]
for i in data_list:
    #print(i)
    if len(i)<6:
        continue
    if len(i)>20:
        specail_ip_range.append(i)
        continue        
    a = IPy.IP(i)
    for j in a:
        # print(j)
        all_white_black.append(str(j))
 
#print(len(all_white_black))
 
# special white_black_ip_range 
 
white_black_special=[]
for x in specail_ip_range:
    #print(x)
    ip0=x.split("-")[0].split(".")[0:3]
    ip1=int(x.split("-")[0].split(".")[3])
    ip2=int(x.split("-")[1].split(".")[3])
    #print(ip1,ip2)
    for l in range(ip1,ip2+1):
 
        #print(ip0)
        ip_a=[i for i in ip0]
        ip_a.append(str(l))
        white_black_special.append(".".join(ip_a))
white_black_special=set(white_black_special)
#print(white_black_special)
all_white_black.extend(white_black_special)
all_white_black=set(all_white_black)
 
# 2. target ip list
target_ip_data = pd.read_excel('xxx/target_ip.xlsx')
target_ip_list = []
for i in range(len(target_ip_data)):
    tmp_li = str(target_ip_data.iloc[i].tolist()[0])
    tmp_lis = tmp_li.split()
    for j in range(len(tmp_lis)):
        target_ip_list.append(tmp_lis[j])
#print(target_ip_list)
 
# 3. judege the target_ip_list is in the all_white_black
result=[]
for i in target_ip_list:
    if i in all_white_black:
        result.append("%s ture
"
% i) else: result.append("%s false
"
% i) f=open('xxx/RESULT_v1.TXT','w') f.writelines(result) f.close() # for i in target_ip_list: # if i in all_white_black: # print("%s ture " % i) # else: # print("%s false" % i) A: ![ ](https://img-blog.csdnimg.cn/20191006113347356.png) B: ![ ](https://img-blog.csdnimg.cn/20191006113359483.png)

좋은 웹페이지 즐겨찾기