从文本文件中打印多行

3
如果我有一个像这样的文本文件:
FastEthernet3/1
ip address 0.0.0.0
enable portfast

FastEthernet3/2
ip address 0.0.0.0
enable portfast

FastEthernet3/3
ip address 0.0.0.0

FastEthernet3/4
ip address 0.0.0.0

我可以帮您进行翻译。这段内容是关于编程的,询问如何在Python中打印未启用Portfast的接口。以下是您提供的代码:
import os
import sys

root = "path to text file like the example above"

os.chdir(root)

current2 = os.getcwd()

print ("CWD = ", current2,"\n")


file = sys.argv[1]

f = open(file)
contents = f.read()
f.close()
print ("Number of GigabitEthernet:",contents.count("interface GigabitEthernet"))
print ("Number of FastEthernet:",contents.count("FastEthernet"))


x = open(file)
string1 = "enable portfast"
for line in x.readlines():
    if line.startswith(string1)
        print (line)
filehandle.close()

所以我可以找到启用Portfast的行并打印它,但我希望它能打印更多行,这样我就知道哪个接口启用了Portfast。

将端口号和启用状态存储,直到达到下一个块或文件结尾,然后您就知道要打印什么。 - Karoly Horvath
line.endswith(string2) 是什么意思? - Alexander
1
我喜欢l4mpi的解决方案,但是有没有带有“interface GigabitEthernet”的接口? - Minion91
是的,但这只是一个示例文本文件,我使用的是思科路由器配置文件。 - DT22
它们都是由空白行分隔的吗? - Minion91
6个回答

2

无法返回文件,因此最好的解决方案是跟踪前面的行,并在端口快速匹配时将其打印出来。

编辑以包括解决方案:(感谢 Alexander)

import re
pinterfaces = re.compile("\r?\n\r?\n").split(contents)
# pinterfaces = ['FastEthernet3/1...', 'FastEthernet3/2...', ...]
for pinterface in pinterfaces:
  if "enable portfast" in pinterface:
    print pinterface

@ Minion91 -- 差别在哪里? :) - root
@root 我喜欢正则表达式,它们让我微笑 :) - Minion91

2
如果每个接口定义都以字符串"FastEthernet"开头,您可以通过该字符串对contents进行分割:
interfaces = contents.split("FastEthernet"):
for interface in interfaces:
    if "enable portfast" in interface:
        print "FastEthernet" + interface

编辑:根据亚历山大(Alexander)的解决方案,如果始终有一个空白行分隔接口,则只需像这样声明interfaces

interfaces = contents.split("\n\n")

...并将打印语句更改为仅打印接口


1
不要忘记在按空行分割时删除 "FastEthernet"。 - Minion91

2

按照基于空行分离的接口进行拆分:

import re
pinterfaces = re.compile("\r?\n\r?\n").split(contents)
# pinterfaces = ['FastEthernet3/1...', 'FastEthernet3/2...', ...]
for pinterface in pinterfaces:
  if "enable portfast" in pinterface:
    print pinterface

FastEthernet3/1
ip address 0.0.0.0
enable portfast
FastEthernet3/2
ip address 0.0.0.0
enable portfast

不需要使用正则表达式,你可以直接使用contents.split("\n\n") - l4mpi

0
with open('test.txt', 'r') as in_file:
    lines = in_file.readlines()

for i,v in enumerate(lines):
    #print i,v
    if v.strip() == 'enable portfast':
        print lines[i-2].strip()
        print lines[i-1].strip()
        print lines[i].strip()

这将打印出:

FastEthernet3/1
ip address 0.0.0.0
enable portfast
FastEthernet3/2
ip address 0.0.0.0
enable portfast

这假设每个匹配的接口定义都恰好有三行。 - l4mpi

0
感谢Alexander,现在它可以工作了 :) 唯一的问题是我的文本文件与示例有点不同。它看起来像这样:
FastEthernet3/1
ip address 0.0.0.0
enable portfast
!
FastEthernet3/2
ip address 0.0.0.0
enable portfast
!
FastEthernet3/3
ip address 0.0.0.0
!
FastEthernet3/4
ip address 0.0.0.0

所以我先用空格替换了感叹号,然后它就正常工作了 :)
import re
contents2 = contents.replace("!","")
pinterfaces = re.compile("\n\n").split(contents2)
# pinterfaces = ['FastEthernet3/1...', 'FastEthernet3/2...', ...]
for pinterface in pinterfaces:
  if "enable portfast" in pinterface:
    print (pinterface)

-1
尝试将所有设备分别解析成字典数组,如下所示:

[{"device_name":"FastEthernet/1","address":"1.0.0.0", "portfast": True}]

接下来,您可以循环遍历此哈希表并打印具有 portfast 值的项目的设备名称。


4
我认为那不是他想要的。 - Minion91
抱歉,在阅读后我进行了修正,现在可能更接近正确了。 - Pablo Jomer
好的解决方案应该是实现一个类,可以读取所有行并将它们解析成哈希表。然后,您可以循环遍历该哈希表,并打印使用启用端口快速功能的设备。 - Pablo Jomer

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接