【问题标题】:Print multiple lines from text file从文本文件中打印多行
【发布时间】:2012-09-20 11:10:35
【问题描述】:

如果我有这样的文本文件:

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

我想打印出没有启用 portfast 的接口。我如何在 python 中打印这个?

我有以下代码:

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。

【问题讨论】:

  • 存储端口号和启用状态,直到您到达下一个块或文件末尾,然后您就知道要打印什么了
  • line.endswith(string2) 是怎么回事?
  • 我喜欢l4mpi的方案,但是有"interface GigabitEthernet"的接口吗?
  • 是的,但这只是一个示例文本文件,我使用的是 cisco 路由器配置文件
  • 都是白线分隔的吗?

标签: python config cisco


【解决方案1】:

基于空行分隔的接口拆分:

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")
【解决方案2】:

无法返回文件,因此最好的解决方案是跟踪前几行并在 portFast 匹配时打印它们。

编辑以包含解决方案:(感谢 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 我喜欢他们让我微笑 :)
【解决方案3】:

如果每个接口定义都以字符串"FastEthernet" 开头,您可以将contents 拆分为该字符串:

interfaces = contents.split("FastEthernet"):
for interface in interfaces:
    if "enable portfast" in interface:
        print "FastEthernet" + interface

编辑:基于 Alexanders 解决方案,如果总是有一个空行分隔接口,只需像这样声明interfaces

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

...并将打印语句更改为仅print interface

【讨论】:

  • 如果在空白行上拆分,请不要忘记删除 "FastEthernet"
【解决方案4】:
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

【讨论】:

  • 这假设每个匹配的接口定义正好有三行。
【解决方案5】:

感谢 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)

【讨论】:

    【解决方案6】:

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

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

    然后您可以遍历此哈希并打印具有portfast 值的项目的设备名称。

    【讨论】:

    • 我不认为这是他想要的
    • 抱歉,我在阅读后更正了它,因为这可能更接近。
    • 好吧,好的解决方案应该是实现一个可以读取所有行并将它们解析为哈希的类。然后您可以遍历该哈希并打印设备,如果它正在使用 enable portfast .
    猜你喜欢
    • 2016-04-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多