[英]how to read a specific txt from txt file and format it into a table using python
i want to extract specific data from txt file like 'Port-channel2''vlan'column and put what is after vlan in this column.我想从 'Port-channel2''vlan'column 之类的 txt 文件中提取特定数据,并将 vlan 之后的内容放在此列中。 this is part of the txt file:
这是 txt 文件的一部分:
VLAN + IP address --------------------23 Interface IP-Address OK? VLAN + IP 地址 --------------------23 接口 IP-Address OK? Method Status Protocol Vlan1 unassigned YES unset up up
Method Status Protocol Vlan1 unassigned YES unset up
Vlan5 10.26.95.33 YES NVRAM up up Vlan5 10.26.95.33 YES NVRAM up up
Vlan20 10.26.93.1 YES NVRAM up up Vlan20 10.26.93.1 YES NVRAM up up
Vlan30 10.26.93.65 YES NVRAM up up Vlan30 10.26.93.65 YES NVRAM up up
Vlan40 10.63.121.251 YES NVRAM down down Vlan40 10.63.121.251 YES NVRAM down down
Vlan50 10.50.50.54 YES NVRAM up up Vlan50 10.50.50.54 YES NVRAM up up
Vlan60 10.26.95.22 YES NVRAM down down Vlan60 10.26.95.22 YES NVRAM down down
Vlan101 10.26.92.1 YES NVRAM up up Vlan101 10.26.92.1 YES NVRAM up up
Vlan131 10.26.81.1 YES NVRAM down down Vlan131 10.26.81.1 YES NVRAM down down
Vlan134 10.26.82.1 YES NVRAM up up Vlan134 10.26.82.1 YES NVRAM up up
Vlan151 10.26.83.1 YES NVRAM down down Vlan151 10.26.83.1 YES NVRAM down down
Vlan201 10.26.80.1 YES NVRAM up up Vlan201 10.26.80.1 YES NVRAM up up
Subnet Mask -------------39 Internet address is 10.210.130.10/30 Internet address is 172.16.1.202/24 Internet address is 151.151.151.151/32 Internet address is 10.26.95.33/27 Internet address is 10.26.93.1/26 Internet address is 10.26.93.65/26 Internet address is 10.63.121.251/28 Internet address is 10.50.50.54/24 Internet address is 10.26.95.22/29 Internet address is 10.26.92.1/24 Internet address is 10.26.81.1/24 Internet address is 10.26.82.1/24 Internet address is 10.26.83.1/24 Internet address is 10.26.80.1/24 ##################################################################################子网掩码 -------------39 外网地址为 10.210.130.10/30 外网地址为 172.16.1.202/24 外网地址为 151.151.151.151/32 外网地址为 10.26.95.33/27 外网地址是 10.26.93.1/26 互联网地址是 10.26.93.65/26 互联网地址是 10.63.121.251/28 互联网地址是 10.50.50.54/24 互联网地址是 10.26.95.22/29 互联网地址是 10.26.92.1/24 互联网地址是 10.26 .81.1/24 互联网地址是 10.26.82.1/24 互联网地址是 10.26.83.1/24 互联网地址是 10.26.80.1/24 ###################### ################################################# ##########
LAN Sheet55 ############ LAN Sheet55 ############
Access or Trunk with VLANs使用 VLAN 访问或中继
interface Port-channel2 switchport trunk allowed vlan 5,20,30,101,134,201,381 interface Port-channel1 interface GigabitEthernet0/2 switchport trunk allowed vlan 5,20,30,101,134,201,381 interface GigabitEthernet0/3 interface GigabitEthernet0/0 interface GigabitEthernet0/1 channel-group 1 mode on interface GigabitEthernet1/0 switchport trunk allowed vlan 5,20,30,101,134,201,381 channel-group 2 mode on70 interface GigabitEthernet1/1 switchport trunk allowed vlan 5,20,30,101,134,201,381 channel-group 2 mode on interface GigabitEthernet1/2 interface GigabitEthernet1/3 ip route 172.16.1.203 255.255.255.255 GigabitEthernet0/1 the expected output is expected output interface Port-channel2 switchport trunk allowed vlan 5,20,30,101,134,201,381 interface Port-channel1 interface GigabitEthernet0/2 switchport trunk allowed vlan 5,20,30,101,134,201,381 interface GigabitEthernet0/3 interface GigabitEthernet0/0 interface GigabitEthernet0/1 interface GigabitEthernet1上的通道组1模式/0 switchport trunk allowed vlan 5,20,30,101,134,201,381 channel-group 2 mode on70 interface GigabitEthernet1/1 switchport trunk allowed vlan 5,20,30,101,134,201,381 channel-group 2 mode on interface GigabitEthernet1/2 interface GigabitEthernet1/3 ip route 172.16.1.203 255.255 .255.255 GigabitEthernet0/1预期 output预期 output
Here is one of many possible ways, but the shortest way (and probably least robust) way I could think of, given the text provided in the question:鉴于问题中提供的文本,这是我能想到的许多可能方法中的一种,但这是我能想到的最短的方法(可能也是最不健全的方法):
def extract_interface_info(config_text):
"""Returns a list of tuples:
[(port id, channel-group, switchport mode,vlans),...]
"""
return [ (interface[0], # port id
interface[interface.index('channel-group')+1]
if 'channel-group' in interface else '',
interface[interface.index('switchport')+1],
interface[interface.index('vlan')+1]
)
for interface in
( interface_str.split()
for interface_str in config_text.split('interface')
if 'switchport' in interface_str and 'vlan' in interface_str
)
]
info = extract_interface_info(text)
print('\n'.join(str(interface) for interface in info))
prints:印刷:
('Port-channel2', '', 'trunk', '5,20,30,101,134,201,381')
('GigabitEthernet0/2', '', 'trunk', '5,20,30,101,134,201,381')
('GigabitEthernet1/0', '2', 'trunk', '5,20,30,101,134,201,381')
('GigabitEthernet1/1', '2', 'trunk', '5,20,30,101,134,201,381')
I used a generator that splits out the text by the keyword "interface" and filtered the resulting strings by keywords required to appear in the desired sections: 'switchport' and 'vlan'.我使用了一个生成器,它通过关键字“interface”分割文本,并通过出现在所需部分中的关键字过滤结果字符串:“switchport”和“vlan”。 Each string meeting the criteria is split by whitespace and fed to the list comprehension statement
每个符合条件的字符串都由空格分隔并馈送到列表理解语句
The list comprehension statement then puts the desired items from the string into a tuple for each interface and returns a list of those tuples.然后,列表理解语句将字符串中的所需项目放入每个接口的元组中,并返回这些元组的列表。
Note: channel-group does not appear in all interfaces, so a conditional statement is needed to only look up the index of 'channel-group' if it exists, otherwise, a null str is returned.注意:channel-group 并非在所有接口中都出现,因此需要条件语句仅查找“channel-group”的索引是否存在,否则返回 null str。
Further Note: since this is only a snippet of the file, if 'switchport' and 'vlan' were to occur outside the desired context, this solution would fail in some way, such as including one or more erroneous tuples or raising an IndexError.进一步注意:由于这只是文件的一个片段,如果“switchport”和“vlan”出现在所需的上下文之外,这个解决方案会以某种方式失败,例如包含一个或多个错误的元组或引发 IndexError。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.