打印特定行之后的行

2024-06-10 07:35:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个函数,它检查一个.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')

Tags: 文件intxtdefaultlinedescriptioninterfacesinterface
1条回答
网友
1楼 · 发布于 2024-06-10 07:35:19

您可以将数据转换为字典列表,然后循环查找它们以找到您要查找的接口(也许还可以进行其他处理):

data = """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"""

with open('D:\Scripting Test/' + Devicess + '.txt') as textfile:
    data = textfile.read()
    interfaces = []
    for line in data.split("\n"):          # go through lines
        k,_ = line.strip().split(" ",1)    # get the first keyword
        if k=="interface":                 # add a dict() for new interfaces
            current = dict()               # which becomes the current one
            interfaces.append(current)
        current[k]=line                    # accumulate keywords in current 

for i in interfaces:              # find interfaces
    if "qos" not in i:            # without a "qos" keyword
        print(i["interface"])     # print name and description
        print(i["description"])

interface GigabitEthernet3/0/5.34
  description 4046

相关问题 更多 >