1、目前工作上有一堆的ip地址,ip是ok的,但是需要找出来不在这里面的其他ip
import os
a = list()
with open('ip.txt','r') as f:
#print(f.readlines())
for line in f.readlines():
a.append(line.strip())
b = []
for i in range(170,251):
b.append('10.2.17.{}'.format(i))
#比较两个列表的相同元素使用 a&b,比较不同元素使用a^b
c = list(set(a)^set(b))
for i in sorted(c):
print(i)
2、读取大文件使用readline()函数
with open('ip.txt','r') as f:
while True:
line = f.readline()
if line:
print(line.strip())
else:
break
3、读取文件时后面带有换行符怎么做?
def GetData(file):
linenew = ""
with open(file,'r') as f:
line = f.read().splitlines()
print(line)