我想知道是否有人能帮我应对这个挑战
我有一个包含以下文件的文件夹:
switch01.txt
switch02.txt
每个文件都有以下内容:
show running-config
Building configuration...
Current configuration : 23611 bytes
!
! Last configuration change at 16:15:20 BST Tue Apr 27 2021 by admin
! NVRAM config last updated at 16:15:33 BST Tue Apr 27 2021 by admin
show version
Cisco IOS Software, C3750 Software (C3750-IPSERVICESK9-M), Version 15.0(2)SE11, RELEASE SOFTWARE (fc3)
Technical Support: http://www.cisco.com/techsupport
Copyright (c) 1986-2017 by Cisco Systems, Inc.
Compiled Sat 19-Aug-17 09:28 by prod_rel_team
ROM: Bootstrap program is C3750 boot loader
show inventory
Interface IP-Address OK? Method Status Protocol
Vlan1 192.168.77.40 YES manual up up
GigabitEthernet1/0/1 unassigned YES unset up up
GigabitEthernet1/0/2 unassigned YES unset up up
GigabitEthernet1/0/3 unassigned YES unset up up
我想用分隔符“show+where”分割文件夹中的每个文件,并用该分隔符命名每个文件。在本例中,每个文件都有3个分隔符:show running config、show version和show inventory
拆分后,应如下所示:
文件夹内容:
switch01$$$show running-config$$$.txt
switch01$$$show version$$$.txt
switch01$$$show inventory$$$.txt
switch02$$$show running-config$$$.txt
switch02$$$show version$$$.txt
switch02$$$show inventory$$$.txt
switch01$$$的内容显示正在运行的配置$$$.txt文本文件:
Building configuration...
Current configuration : 23611 bytes
!
! Last configuration change at 16:15:20 BST Tue Apr 27 2021 by admin
! NVRAM config last updated at 16:15:33 BST Tue Apr 27 2021 by admin
switch01$$$的内容显示版本$$$.txt文本文件:
Cisco IOS Software, C3750 Software (C3750-IPSERVICESK9-M), Version 15.0(2)SE11, RELEASE SOFTWARE (fc3)
Technical Support: http://www.cisco.com/techsupport
Copyright (c) 1986-2017 by Cisco Systems, Inc.
Compiled Sat 19-Aug-17 09:28 by prod_rel_team
ROM: Bootstrap program is C3750 boot loader
switch01$$$的内容显示库存$$$.txt文本文件:
Interface IP-Address OK? Method Status Protocol
Vlan1 192.168.77.40 YES manual up up
GigabitEthernet1/0/1 unassigned YES unset up up
GigabitEthernet1/0/2 unassigned YES unset up up
GigabitEthernet1/0/3 unassigned YES unset up up
switch02.txt也是如此
我将这段代码拼凑在一起,使用计数器对每个文件进行唯一命名:
if filename.endswith(".txt"):
with open(filename,'r') as file:
output = file.read()
for command in output:
splitter = re.split('show\s(?:running-config|version|inventory)',output, flags=re.IGNORECASE)
counter=0
for item in splitter:
with open(filename.replace(".txt","")+'$$$'+str(counter)+'$$$'+'.txt', 'w') as f:
f.write("%s\n" % item)
counter = counter + 1
这使文件看起来像这样:
switch01$$$0$$$.txt
switch01$$$1$$$.txt
switch01$$$2$$$.txt
switch01$$$3$$$.txt
同样在这一点上,switch01$$$0$$$.txt是空的,不需要
用正则表达式中使用的分隔符命名每个文件需要做什么
使用lark,您可以将
switch*.txt
转换为json
数据,然后按照您想要的方式进行处理相关问题 更多 >
编程相关推荐