首页 > 解决方案 > 将文本解析为列

问题描述

我有一系列路由器的以下输出:

hostname CPE-ABCDEFGHI-55553-03
Interface                  IP-Address      OK? Method Status                Protocol
FastEthernet0              unassigned      YES unset  up                    up      
FastEthernet1              unassigned      YES unset  up                    down    
FastEthernet2              unassigned      YES unset  up                    down    
FastEthernet3              unassigned      YES unset  up                    up      
FastEthernet4              unassigned      YES NVRAM  up                    up      
FastEthernet4.100          93.62.88.112    YES DHCP   up                    up      
FastEthernet4.106          182.21.200.233  YES DHCP   up                    up      
Vlan1                      12.230.1.34     YES NVRAM  up                    up      
###############################################
hostname CPE-XXXXXXXXX-33333-01
Interface                  IP-Address      OK? Method Status                Protocol
Async1                     unassigned      YES unset  down                  down    
FastEthernet0              unassigned      YES unset  up                    up      
FastEthernet1              unassigned      YES unset  down                  down    
FastEthernet2              unassigned      YES unset  down                  down    
FastEthernet3              unassigned      YES unset  down                  down    
FastEthernet4              unassigned      YES unset  down                  down    
FastEthernet5              unassigned      YES unset  down                  down    
FastEthernet6              unassigned      YES unset  down                  down    
FastEthernet7              unassigned      YES unset  down                  down    
FastEthernet8              unassigned      YES NVRAM  administratively down down    
GigabitEthernet0           unassigned      YES DHCP   up                    up      
GigabitEthernet0.100       1.1.1.1    YES DHCP   up                    up      
GigabitEthernet0.106       152.21.133.171  YES DHCP   up                    up      
NVI0                       unassigned      YES unset  administratively down down    
Vlan1                      10.241.0.162    YES NVRAM  up                    up      
Vlan10                     10.240.0.194    YES NVRAM  up                    up      
###############################################
hostname CPE-YYYYYYYYY-28886-03
Interface                  IP-Address      OK? Method Status                Protocol
Cellular0                  unassigned      YES NVRAM  up                    up      
Dialer1                    192.168.255.3   YES IPCP   up                    up      
FastEthernet0              unassigned      YES unset  up                    up      
FastEthernet1              unassigned      YES unset  up                    down    
FastEthernet2              unassigned      YES unset  up                    down    
FastEthernet3              unassigned      YES unset  up                    down    
FastEthernet4              unassigned      YES NVRAM  administratively down down    
Loopback0                  173.17.18.193   YES NVRAM  up                    up      
NVI0                       18.10.10.1      YES unset  up                    up      
Vlan1                      10.241.0.163    YES NVRAM  up                    up      
Vlan10                     10.240.0.195    YES NVRAM  up                    up      
###############################################
hostname CPE-AAAAAAAAA-22222-03
Interface                  IP-Address      OK? Method Status                Protocol
FastEthernet0              unassigned      YES unset  up                    up      
FastEthernet1              unassigned      YES unset  up                    down    
FastEthernet2              unassigned      YES unset  down                  down    
FastEthernet3              unassigned      YES unset  up                    down    
FastEthernet4              unassigned      YES NVRAM  up                    up      
FastEthernet4.100          46.189.234.214  YES DHCP   up                    up      
FastEthernet4.106          172.21.128.240  YES DHCP   up                    up      
Vlan1                      10.241.7.253    YES NVRAM  up                    up      
Vlan10                     10.270.1.35     YES NVRAM  up                    up      
###############################################

我需要将其转换为每个界面有一列的 Excel 文件。为此,我需要制作一个 python 2.7.5(ssh 服务器限制)脚本来将文本解析为 Excel。

例如(想象这些是细胞)

主机名 界面 IP地址 好的? 方法 地位 协议
CPE-ABCDEFGHI-55553-03 快速以太网0 未分配 (...) ... ... ...
CPE-ABCDEFGHI-55553-03 快速以太网1 未分配 (...) ... ... ...

你能帮我做这件事吗?

标签: pythonpython-2.7parsing

解决方案


也许是这样的?

output = []
with open('Output.txt','r') as fil:
    for line in fil:
        output.append(line.strip())

mydata = []
host_curr = ''

for element in output:
    if element.startswith('####') or element.startswith('Interface'):
        continue
    if element.startswith('hostname'):
        host_curr = element.split()[1]
        continue
    myline = host_curr + '\t' + element
    mydata.append(myline)


with open('Output.csv','w') as out:
    for elem in mydata:
        out.write('{}\n'.format(elem))

如果输出是您的输出(这里是文本行列表)。csv 应该是这样的:

CPE-ABCDEFGHI-55553-03  FastEthernet0              unassigned      YES unset  up                    up
CPE-ABCDEFGHI-55553-03  FastEthernet1              unassigned      YES unset  up                    down
CPE-ABCDEFGHI-55553-03  FastEthernet2              unassigned      YES unset  up                    down
CPE-ABCDEFGHI-55553-03  FastEthernet3              unassigned      YES unset  up                    up
CPE-ABCDEFGHI-55553-03  FastEthernet4              unassigned      YES NVRAM  up                    up
CPE-ABCDEFGHI-55553-03  FastEthernet4.100          93.62.88.112    YES DHCP   up                    up
CPE-ABCDEFGHI-55553-03  FastEthernet4.106          182.21.200.233  YES DHCP   up                    up
CPE-ABCDEFGHI-55553-03  Vlan1                      12.230.1.34     YES NVRAM  up                    up
CPE-XXXXXXXXX-33333-01  Async1                     unassigned      YES unset  down                  down
CPE-XXXXXXXXX-33333-01  FastEthernet0              unassigned      YES unset  up                    up
CPE-XXXXXXXXX-33333-01  FastEthernet1              unassigned      YES unset  down                  down
CPE-XXXXXXXXX-33333-01  FastEthernet2              unassigned      YES unset  down                  down
CPE-XXXXXXXXX-33333-01  FastEthernet3              unassigned      YES unset  down                  down
CPE-XXXXXXXXX-33333-01  FastEthernet4              unassigned      YES unset  down                  down
CPE-XXXXXXXXX-33333-01  FastEthernet5              unassigned      YES unset  down                  down
CPE-XXXXXXXXX-33333-01  FastEthernet6              unassigned      YES unset  down                  down
CPE-XXXXXXXXX-33333-01  FastEthernet7              unassigned      YES unset  down                  down
CPE-XXXXXXXXX-33333-01  FastEthernet8              unassigned      YES NVRAM  administratively down down
CPE-XXXXXXXXX-33333-01  GigabitEthernet0           unassigned      YES DHCP   up                    up
CPE-XXXXXXXXX-33333-01  GigabitEthernet0.100       1.1.1.1    YES DHCP   up                    up
CPE-XXXXXXXXX-33333-01  GigabitEthernet0.106       152.21.133.171  YES DHCP   up                    up
CPE-XXXXXXXXX-33333-01  NVI0                       unassigned      YES unset  administratively down down
CPE-XXXXXXXXX-33333-01  Vlan1                      10.241.0.162    YES NVRAM  up                    up
CPE-XXXXXXXXX-33333-01  Vlan10                     10.240.0.194    YES NVRAM  up                    up

推荐阅读