获取 txt 文件中不是第一部分或不存在于 python 中另一个文件行中的行
Get the lines of txt file which not the first part or not exist in another file's lines in python
我有两个 txt 文件,需要在新的 txt 文件中获取输出:
一个文件具有以下内容(名为 f.txt):
interface Eth-Trunk50
interface Eth-Trunk51
interface Eth-Trunk60
interface Eth-Trunk60.2535
interface Eth-Trunk100
interface GigabitEthernet0/0/0
interface GigabitEthernet1/1/1
interface GigabitEthernet1/1/4
interface GigabitEthernet1/1/10
另一个文件包含以下行(名为 x.txt):
interface Eth-Trunk50.1000
interface Eth-Trunk50.1030
interface Eth-Trunk51.2071
interface Eth-Trunk51.2072
interface Eth-Trunk100.108
interface Eth-Trunk100.109
interface Eth-Trunk100.111
interface GigabitEthernet1/1/0
interface GigabitEthernet1/1/1.660
interface GigabitEthernet1/1/1.662
interface GigabitEthernet1/1/2
interface GigabitEthernet1/1/10.3400
interface GigabitEthernet1/1/10.3600
interface GigabitEthernet1/1/10.3800
interface GigabitEthernet2/1/1
interface GigabitEthernet2/1/1.3010
我需要获取 f.txt 文件中不是第一部分或不存在于 x.txt 行中的行以获得如下输出:
interface Eth-Trunk60
interface Eth-Trunk60.2535
interface GigabitEthernet0/0/0
interface GigabitEthernet1/1/4
这是我的 python 代码,但它对我不起作用,所以谢谢你的帮助。
def readB(Devices):
nums = []
with open('C:/Users/Ahmed Shouaib/Downloads/Backup Configurtion files/'+Devices+'-NO trust upstream default33.txt') as f:
nums = f.read().split('\n')
nums = [n.strip() for n in nums if n !='']
with open('C:/Users/Ahmed Shouaib/Downloads/Backup Configurtion files/'+Devices+'-NO trust upstream default55.txt') as x , open('C:/Users/Ahmed Shouaib/Downloads/Backup Configurtion files/' + Devices + '-NO trust upstream default66.txt','w') as new :
for n in nums:
for line in x :
if not line.startswith(n) :
print (n)
new.write(n+'\n')
break
readB('10.0.130.13')
从两个文件中读取所有行
删除每行的最后一个'\n'
检查行是否有效,在这里使用valid
标志:
初始化valide
标志为'True'
如果任何行以检查行开始,标记valid
标记为 'False'
通过 valid
标志过滤行
- 将结果写入结果文件
def readB(Devices):
fLines = open('./f.txt').readlines()
xLines = open('./x.txt').readlines()
with open('./result.txt', 'w') as new:
for fLine in fLines:
fLine = fLine.replace('\n', '')
valid = True
for xLine in xLines:
xLine = xLine.replace('\n', '')
if xLine.startswith(fLine):
valid = False
break
if valid:
print(fLine)
new.write(fLine + '\n')
readB('10.0.130.13')
在这种情况下,您需要对文件进行一些预处理
所以像这样:
def readB(Devices):
new_lines = []
with open('C:/Users/Ahmed Shouaib/Downloads/Backup Configurtion files/'+Devices+'-NO trust upstream default33.txt', "r") as f:
nums = [n.strip().split(".")[0] for n in f.read().split('\n') if n !='']
with open('C:/Users/Ahmed Shouaib/Downloads/Backup Configurtion files/'+Devices+'-NO trust upstream default55.txt', "r") as x:
lines = [n.strip().split(".")[0] for n in x.read().split('\n') if n !='']
for n in nums:
if n not in lines:
new_lines.append(n)
with open('C:/Users/Ahmed Shouaib/Downloads/Backup Configurtion files/' + Devices + '-NO trust upstream default66.txt','w') as new:
new.write("\n".join(new_lines))
readB('10.0.130.13')
我有两个 txt 文件,需要在新的 txt 文件中获取输出:
一个文件具有以下内容(名为 f.txt):
interface Eth-Trunk50
interface Eth-Trunk51
interface Eth-Trunk60
interface Eth-Trunk60.2535
interface Eth-Trunk100
interface GigabitEthernet0/0/0
interface GigabitEthernet1/1/1
interface GigabitEthernet1/1/4
interface GigabitEthernet1/1/10
另一个文件包含以下行(名为 x.txt):
interface Eth-Trunk50.1000
interface Eth-Trunk50.1030
interface Eth-Trunk51.2071
interface Eth-Trunk51.2072
interface Eth-Trunk100.108
interface Eth-Trunk100.109
interface Eth-Trunk100.111
interface GigabitEthernet1/1/0
interface GigabitEthernet1/1/1.660
interface GigabitEthernet1/1/1.662
interface GigabitEthernet1/1/2
interface GigabitEthernet1/1/10.3400
interface GigabitEthernet1/1/10.3600
interface GigabitEthernet1/1/10.3800
interface GigabitEthernet2/1/1
interface GigabitEthernet2/1/1.3010
我需要获取 f.txt 文件中不是第一部分或不存在于 x.txt 行中的行以获得如下输出:
interface Eth-Trunk60
interface Eth-Trunk60.2535
interface GigabitEthernet0/0/0
interface GigabitEthernet1/1/4
这是我的 python 代码,但它对我不起作用,所以谢谢你的帮助。
def readB(Devices):
nums = []
with open('C:/Users/Ahmed Shouaib/Downloads/Backup Configurtion files/'+Devices+'-NO trust upstream default33.txt') as f:
nums = f.read().split('\n')
nums = [n.strip() for n in nums if n !='']
with open('C:/Users/Ahmed Shouaib/Downloads/Backup Configurtion files/'+Devices+'-NO trust upstream default55.txt') as x , open('C:/Users/Ahmed Shouaib/Downloads/Backup Configurtion files/' + Devices + '-NO trust upstream default66.txt','w') as new :
for n in nums:
for line in x :
if not line.startswith(n) :
print (n)
new.write(n+'\n')
break
readB('10.0.130.13')
从两个文件中读取所有行
删除每行的最后一个
'\n'
检查行是否有效,在这里使用
valid
标志:
初始化
valide
标志为'True'如果任何行以检查行开始,标记
valid
标记为 'False'通过
valid
标志过滤行
- 将结果写入结果文件
def readB(Devices):
fLines = open('./f.txt').readlines()
xLines = open('./x.txt').readlines()
with open('./result.txt', 'w') as new:
for fLine in fLines:
fLine = fLine.replace('\n', '')
valid = True
for xLine in xLines:
xLine = xLine.replace('\n', '')
if xLine.startswith(fLine):
valid = False
break
if valid:
print(fLine)
new.write(fLine + '\n')
readB('10.0.130.13')
在这种情况下,您需要对文件进行一些预处理
所以像这样:
def readB(Devices):
new_lines = []
with open('C:/Users/Ahmed Shouaib/Downloads/Backup Configurtion files/'+Devices+'-NO trust upstream default33.txt', "r") as f:
nums = [n.strip().split(".")[0] for n in f.read().split('\n') if n !='']
with open('C:/Users/Ahmed Shouaib/Downloads/Backup Configurtion files/'+Devices+'-NO trust upstream default55.txt', "r") as x:
lines = [n.strip().split(".")[0] for n in x.read().split('\n') if n !='']
for n in nums:
if n not in lines:
new_lines.append(n)
with open('C:/Users/Ahmed Shouaib/Downloads/Backup Configurtion files/' + Devices + '-NO trust upstream default66.txt','w') as new:
new.write("\n".join(new_lines))
readB('10.0.130.13')