字符串转换矩阵python



在此处输入图像描述如何将以下输出转换为数组或矩阵。

因此,为了将其放入数据帧中,我会这样做。

import pandas as pd 
txt= """Interface IP-Address OK? Method Status Protocol
Vlan1 unassigned YES NVRAM administratively down down
Vlan5 10.0.5.4 YES NVRAM up up
Vlan16 unassigned YES NVRAM up up
Vlan19 172.16.205.205 YES NVRAM up up
Vlan33 172.16.33.4 YES NVRAM up up
Vlan41 192.168.11.4 YES NVRAM up up
Vlan192 192.168.10.4 YES NVRAM up up
Vlan200 192.168.3.4 YES NVRAM up up
GigabitEthernet0/1 unassigned YES unset down down
GigabitEthernet0/2 unassigned YES unset down down
GigabitEthernet0/3 unassigned YES unset down down
GigabitEthernet0/4 unassigned YES unset down down
GigabitEthernet0/5 unassigned YES unset up up
GigabitEthernet0/6 unassigned YES unset down down
GigabitEthernet0/7 unassigned YES unset down down
GigabitEthernet0/8 unassigned YES unset up up
GigabitEthernet0/9 unassigned YES unset up up
GigabitEthernet0/10 unassigned YES unset down down
GigabitEthernet0/11 unassigned YES unset down down
GigabitEthernet0/12 unassigned YES unset up up
GigabitEthernet0/13 unassigned YES unset administratively down down
GigabitEthernet0/14 unassigned YES unset down down
GigabitEthernet0/15 unassigned YES unset down down 
GigabitEthernet0/16 unassigned YES unset down down 
GigabitEthernet0/17 unassigned YES unset up up 
GigabitEthernet0/18 unassigned YES unset up up 
GigabitEthernet0/19 unassigned YES unset up up 
GigabitEthernet0/20 unassigned YES unset down down 
GigabitEthernet0/21 unassigned YES unset up up 
GigabitEthernet0/22 unassigned YES unset up up 
GigabitEthernet0/23 unassigned YES unset up up 
GigabitEthernet0/24 unassigned YES unset down down 
GigabitEthernet0/25 unassigned YES unset up up"""

#split endlines and subsequently split on spaces
#use filter(None,split_line) is used to remove any accidental lone spaces what would result in " "
split_text = [list(filter(None,line.split(" "))) for line in txt.replace("r","").split("n")]
#if split on spaces adminstarively up/down will be a problem as it would make for length 7, so if length 7 combine the words.
split_text = [[split_lines[0],split_lines[1],split_lines[2],split_lines[3],split_lines[4] + " "+ split_lines[5],split_lines[6]] if len(split_lines) > 6 else split_lines for split_lines in split_text]
#skip first line (as those are the columns) 
dataframe = pd.DataFrame(split_text[1:],columns=split_text[0])
print(dataframe.head(10)) #prints the first 10 elements

前10个元素的输出是这样的。

Interface      IP-Address  OK? Method                 Status Protocol
0               Vlan1      unassigned  YES  NVRAM  administratively down     down
1               Vlan5        10.0.5.4  YES  NVRAM                     up       up
2              Vlan16      unassigned  YES  NVRAM                     up       up
3              Vlan19  172.16.205.205  YES  NVRAM                     up       up
4              Vlan33     172.16.33.4  YES  NVRAM                     up       up
5              Vlan41    192.168.11.4  YES  NVRAM                     up       up
6             Vlan192    192.168.10.4  YES  NVRAM                     up       up
7             Vlan200     192.168.3.4  YES  NVRAM                     up       up
8  GigabitEthernet0/1      unassigned  YES  unset                   down     down
9  GigabitEthernet0/2      unassigned  YES  unset                   down     down

最新更新