我有一个函数,它检查一个.txt文件,并在分析后从中获取一些数据。 我有一个名为(Data.txt)的文件,其中包含以下内容:
interface GigabitEthernet3/0/5.33
description 4543
trust upstream default
trust 8021p outbound
qos phb dscp disable
interface GigabitEthernet3/0/5.34
description 4046
trust upstream default
trust 8021p outbound
interface GigabitEthernet3/0/5.35
description 4584
trust upstream default
trust 8021p outbound
qos phb dscp disable
下面的功能是提取其下没有“qos phb dscp disable”的接口。 因此,最终结果应该保存在一个文件中(没有qos.txt的数据) 其中包含“GigabitEthernet3/0/5.34接口”
我要求的内容:我已尝试打印带有说明的界面,因此结果将是:
interface GigabitEthernet3/0/5.34
description 4046
有人能帮我吗
def noqos(Devicess):
data = open('D:\Scripting Test/' + Devicess + '.txt').read()
def no_qos(lines):
# keep track of interfaces seen and which has qos
interfaces = []
has_qos = set()
print (Devicess + '( No qos under interfaces )')
print ("-----------------------------------------------------------")
# scan the file and gather interfaces and which have qos
for line in lines:
if line.startswith('interface'):
interface = line.strip()
interfaces.append(interface)
elif line.startswith(" qos"):
has_qos.add(interface)
# report which interfaces do not have qos
return [i for i in interfaces if i not in has_qos]
lastnoqos = open(('D:\Scripting Test/Noqos/' + Devicess + ' no qos.txt'), "w")
for interface in no_qos(data.split('\n')):
# print(interface)
# print ("\n")
lastnoqos.write(interface + '\n')
noqos('Data')
您可以将数据转换为字典列表,然后循环查找它们以找到您要查找的接口(也许还可以进行其他处理):
相关问题 更多 >
编程相关推荐