标签:批量 write new == main pytho type 判断 spl
# -*- coding: utf-8 -*- """ @Time : 2020/1/3 16:32 @Auth : oliver @File :ip-c.py @IDE :PyCharm """ def ipToc(ip): ip_c =ip.split(‘.‘) # print len(ip_c) nei_ip=(10,192,172,127) # 判断是IP分割后数组长度,防止空行,数组越界问题 if len(ip_c)==4: # 去除内网IP # print type(ip_c[0]) if int(ip_c[0]) not in nei_ip: # print ip_c # print ip_c[0] + ‘.‘ + ip_c[1] + ‘.‘ + ip_c[2] + ‘.1/24‘ l= ip_c[0] + ‘.‘ + ip_c[1] + ‘.‘ + ip_c[2] + ‘.1/24‘ with open(‘ip_c.txt‘,‘a+‘) as f: # 防止重复,判断不存在,然后写入文件 if l not in f: f.writelines(l+‘\n‘) if __name__ == ‘__main__‘: with open(‘ip.txt‘, ‘r‘) as f: for line in f: line = line.strip(‘\n‘) ipToc(line)
用于批量将IP(X.X.X.X)转换为(X.X.X.1/24)
标签:批量 write new == main pytho type 判断 spl
原文地址:https://www.cnblogs.com/oliver-yt/p/12817289.html