我想知道这里的问题是否可以通过一个正则表达式来解决,或者我应该进行标准循环并逐行评估,
当我运行包含的代码时,我得到的['Ethernet0/22', 'Ethernet0/24']结果应该是['Ethernet0/23', 'Ethernet0/25'].
有什么建议吗?
import re
txt='''#
interface Ethernet0/22
stp disable
broadcast-suppression 5
mac-address max-mac-count 1
port access vlan 452
#
interface Ethernet0/23
stp disable
description BTO
broadcast-suppression 5
port access vlan 2421
#
interface Ethernet0/24
stp disable
description Avaya G700
broadcast-suppression 5
port access vlan 452
#
interface Ethernet0/25
stp disable
description BTO
broadcast-suppression 5
port access vlan 2421
#
'''
re1 = '''^interface (.*?$).*?BTO.*?^#$'''
rg = re.compile(re1,re.IGNORECASE|re.DOTALL|re.MULTILINE)
m = rg.findall(txt)
if m:
print m
Run Code Online (Sandbox Code Playgroud)
这是您的文件的一个小pyparsing解析器.这不仅显示了您当前问题的解决方案,而且解析器为您提供了一组很好的对象,您可以使用这些对象轻松访问每个接口中的数据.
这是解析器:
from pyparsing import *
# set up the parser
comment = "#" + Optional(restOfLine)
keyname = Word(alphas,alphanums+'-')
value = Combine(empty + SkipTo(LineEnd() | comment))
INTERFACE = Keyword("interface")
interfaceDef = Group(INTERFACE + value("name") + \
Dict(OneOrMore(Group(~INTERFACE + keyname + value))))
# ignore comments (could be anywhere)
interfaceDef.ignore(comment)
# parse the source text
ifcdata = OneOrMore(interfaceDef).parseString(txt)
Run Code Online (Sandbox Code Playgroud)
现在该如何使用它:
# use dump() to list all of the named fields created at parse time
for ifc in ifcdata:
print ifc.dump()
# first the answer to the OP's question
print [ifc.name for ifc in ifcdata if ifc.description == "BTO"]
# how to access fields that are not legal Python identifiers
print [(ifc.name,ifc['broadcast-suppression']) for ifc in ifcdata
if 'broadcast-suppression' in ifc]
# using names to index into a mapping with string interpolation
print ', '.join(["(%(name)s, '%(port)s')" % ifc for ifc in ifcdata ])
Run Code Online (Sandbox Code Playgroud)
打印出来:
['interface', 'Ethernet0/22', ['stp', 'disable'], ['broadcast-suppression', '5'], ['mac-address', 'max-mac-count 1'], ['port', 'access vlan 452']]
- broadcast-suppression: 5
- mac-address: max-mac-count 1
- name: Ethernet0/22
- port: access vlan 452
- stp: disable
['interface', 'Ethernet0/23', ['stp', 'disable'], ['description', 'BTO'], ['broadcast-suppression', '5'], ['port', 'access vlan 2421']]
- broadcast-suppression: 5
- description: BTO
- name: Ethernet0/23
- port: access vlan 2421
- stp: disable
['interface', 'Ethernet0/24', ['stp', 'disable'], ['description', 'Avaya G700'], ['broadcast-suppression', '5'], ['port', 'access vlan 452']]
- broadcast-suppression: 5
- description: Avaya G700
- name: Ethernet0/24
- port: access vlan 452
- stp: disable
['interface', 'Ethernet0/25', ['stp', 'disable'], ['description', 'BTO'], ['broadcast-suppression', '5'], ['port', 'access vlan 2421']]
- broadcast-suppression: 5
- description: BTO
- name: Ethernet0/25
- port: access vlan 2421
- stp: disable
['Ethernet0/23', 'Ethernet0/25']
[('Ethernet0/22', '5'), ('Ethernet0/23', '5'), ('Ethernet0/24', '5'), ('Ethernet0/25', '5')]
(Ethernet0/22, 'access vlan 452'), (Ethernet0/23, 'access vlan 2421'), (Ethernet0/24, 'access vlan 452'), (Ethernet0/25, 'access vlan 2421')
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1887 次 |
| 最近记录: |