python循环搜索文本中的特定分隔线

2024-05-18 05:51:13 发布

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

我是一个初学者python学习和被困在这个问题。。。如果您能提供任何关于编写python脚本的专家建议和帮助,我将不胜感激,该脚本可以通过查看下面一组/一组行中的特定分隔符来读取和搜索配置文件。你知道吗

开始分隔符:^vpls\s\d+
结束分隔符:^!$

##This is an example subset of an Alcatel router config:
!
vpls xxx
sdp 1.1.1.1
backup
lsp LSP-1
... #some other settings here (not needed)
exit
sdp 2.2.2.2
lsp LSP-2
... #some other settings here (not needed)
exit
!
vpls yyy
sdp 1.1.1.1
backup
lsp LSP-1
... #some other settings here (not needed)
exit
sdp 2.2.2.2
lsp LSP-2
... #some other settings here (not needed)
exit
!

我想要实现的是,请注意,在导出到csv后,备份在标准格式序列的最后一项上传输:

vpls xxx : sdp 1.1.1.1 : lsp LSP-1 : backup
vpls xxx : sdp 2.2.2.2 : lsp LSP-2 : 
vpls yyy : sdp 1.1.1.1 : lsp LSP-1 : backup
vpls yyy : sdp 2.2.2.2 : lsp LSP-2 : 

Tags: sdpheresettingsexitnotsomebackupxxx
1条回答
网友
1楼 · 发布于 2024-05-18 05:51:13

我做了这个剧本。在router_cfg中,我存储了您提供的输出。你知道吗

r = re.compile('(?:(vpls\w+)\s)?(sdp [\w\.]+)\s(?:(backup)\s)?(lsp [\w-]+)')
for line in r.findall(router_cfg):
    if line[0]: vpls = line[0]
    print ' : '.join([vpls, line[1],line[3],line[2]])

如果你有什么不明白的,尽管问。你知道吗

相关问题 更多 >