使用IPy模块转换,并去重输出到文件
# -*- coding: UTF-8 -*- import IPy def ipToC(): ips = set() readPath = 'ip.txt' writePath = 'ip_c.txt' outFile = open(writePath, 'w') with open(readPath, 'r') as f: for line in f: ip = line.strip(' ') ip_c = IPy.IP(ip).make_net('255.255.255.0') if ip_c not in ips: outFile.write(str(ip_c) + ' ') ips.add(ip_c) outFile.close() print '转换结束' if __name__ == '__main__': ipToC()