0

我正在尝试读取文本文件并将其中的特定部分提取到 CSV 文件中,

#**This is the part of the txt file:**#
- 172.16.1.202
- Hostaname: S01
#################################################################################
- VLAN-IP addressing Sheet
##########################
- VLAN + Description
--------------------
Interface                      Status         Protocol Description
Vl1                            up             up       
Vl5                            up             up       Legacy-RC-Admin
Vl20                           up             up       Legacy-RC-server
Vl30                           up             up       Legacy-RC-iSCSI
Vl40                           down           down     WAN Interconnect VLAN
Vl50                           up             up       
Vl60                           down           down     Tech FW ICS
Vl101                          up             up       RFR Data
Vl131                          down           down     Data WLAN
Vl134                          up             up       WLAN Management
Vl151                          down           down     Factory WLAN
Vl201                          up             up       RFR Management

- VLAN + IP address
--------------------
Interface              IP-Address      OK? Method Status                Protocol
Vlan1                  unassigned      YES unset  up                    up      
Vlan5                  10.26.95.33     YES NVRAM  up                    up      
Vlan20                 10.26.93.1      YES NVRAM  up                    up      
Vlan30                 10.26.93.65     YES NVRAM  up                    up      
Vlan40                 10.63.121.251   YES NVRAM  down                  down    
Vlan50                 10.50.50.54     YES NVRAM  up                    up      
Vlan60                 10.26.95.22     YES NVRAM  down                  down    
Vlan101                10.26.92.1      YES NVRAM  up                    up      
Vlan131                10.26.81.1      YES NVRAM  down                  down    
Vlan134                10.26.82.1      YES NVRAM  up                    up      
Vlan151                10.26.83.1      YES NVRAM  down                  down    
Vlan201                10.26.80.1      YES NVRAM  up                    up

- Subnet Mask
-------------
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
##################################################################################
- LAN Sheet
############
- Access or Trunk with VLANs
----------------------------
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 on
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

- Non user interface with Description
-------------------------------------
 Interface                      Status         Protocol Description
Gi0/2                          up             up       Network link to eff-e-rfr-sw-as1
Gi0/3                          up             up       
Gi0/0                          up             up       
Gi0/1                          up             up       Network link to eff-e-rfr-sw-as1
Gi1/0                          up             up       Network link to eff-e-rfr-sw-as2
Gi1/1                          up             up       Network link to eff-e-rfr-sw-as2
Gi1/2                          up             up       
Gi1/3                          up             up       
Po2                            down           down     Network link to eff-e-rfr-sw-as2
Po1                            up             up       Network link to eff-e-rfr-sw-as1
Lo50                           up             up

- Remote port
---------------
Interface: GigabitEthernet0/0,  Port ID (outgoing port): Ethernet0/0
Interface: GigabitEthernet0/0,  Port ID (outgoing port): GigabitEthernet0/0
Interface: GigabitEthernet0/2,  Port ID (outgoing port): GigabitEthernet0/2
Interface: GigabitEthernet0/0,  Port ID (outgoing port): GigabitEthernet0/0
Interface: GigabitEthernet0/0,  Port ID (outgoing port): GigabitEthernet0/0
Interface: GigabitEthernet0/1,  Port ID (outgoing port): GigabitEthernet0/1
Interface: GigabitEthernet0/0,  Port ID (outgoing port): GigabitEthernet0/0
Interface: GigabitEthernet0/0,  Port ID (outgoing port): GigabitEthernet0/0
Interface: GigabitEthernet1/1,  Port ID (outgoing port): GigabitEthernet1/1
Interface: GigabitEthernet1/0,  Port ID (outgoing port): GigabitEthernet1/0
Interface: GigabitEthernet0/0,  Port ID (outgoing port): GigabitEthernet0/1
Interface: GigabitEthernet0/0,  Port ID (outgoing port): GigabitEthernet0/0
Interface: GigabitEthernet0/0,  Port ID (outgoing port): mgmt0
Interface: GigabitEthernet0/0,  Port ID (outgoing port): GigabitEthernet0/0/0/0

预期输出为: 预期输出

4

2 回答 2

1

您可以使用pandas.read_csv()

  • skiprows=8跳过前 8 行
  • nrows=12读取 12 行
  • sep='\s{2,}'使用多个空格作为分隔符
  • names=['Interface','Status','Protocol','Description']手动设置列名,因为最后两个列名之间的单个空格会干扰所需的多空格分隔符,因为最后一行包含单元格中的空格。

下一部分也是如此,具有调整的行值。在这里,我们可以delim_whitespace=True使用所有空格作为分隔符,因为值中没有包含空格的列。pandas现在还可以自动推断列名,因为此表中没有带空格的值。

最后,join两个数据框:

import pandas as pd

df1 = pd.read_csv('filename.txt', skiprows=8, nrows=12, sep='\s{2,}', names=['Interface','Status','Protocol','Description'])
df2 = pd.read_csv('filename.txt', skiprows=23, nrows=12, delim_whitespace=True)
final_df = df1.join(df2, lsuffix='_decription', rsuffix='_ipaddress')

结果:

接口描述 状态描述 协议描述 描述 接口IP地址 IP地址 好的? 方法 状态_ipaddress 协议IP地址
0 Vl1 向上 向上 Vlan1 未分配 是的 未设置 向上 向上
1 Vl5 向上 向上 旧版-RC-Admin Vlan5 10.26.95.33 是的 非易失性存储器 向上 向上
2 VL20 向上 向上 旧版 RC 服务器 Vlan20 10.26.93.1 是的 非易失性存储器 向上 向上
3 VL30 向上 向上 传统-RC-iSCSI Vlan30 10.26.93.65 是的 非易失性存储器 向上 向上
4 VL40 WAN 互连 VLAN Vlan40 10.63.121.251 是的 非易失性存储器
5 VL50 向上 向上 Vlan50 10.50.50.54 是的 非易失性存储器 向上 向上
6 VL60 技术固件 ICS Vlan60 10.26.95.22 是的 非易失性存储器
7 VL101 向上 向上 射频数据 Vlan101 10.26.92.1 是的 非易失性存储器 向上 向上
8 VL131 数据 WLAN vlan131 10.26.81.1 是的 非易失性存储器
9 VL134 向上 向上 无线局域网管理 vlan134 10.26.82.1 是的 非易失性存储器 向上 向上
10 VL151 工厂无线局域网 Vlan151 10.26.83.1 是的 非易失性存储器
11 VL201 向上 向上 RFR 管理 vlan201 10.26.80.1 是的 非易失性存储器 向上 向上

您可以将数据框保存到新csv文件:final_df.to_csv('filename.csv')

于 2021-05-06T19:50:11.200 回答
0

转到此链接Turning a text file into a tabular format我认为您正在寻找相同的答案,谢谢,那里已经解释过了。

于 2021-05-06T19:37:54.097 回答