How to convert a text into a tabular format

73 Views Asked by At

I want to convert a txt file into a tabular format. I used pandas to skip rows but I want a way that will be generic not by skipping rows:

This is the txt file:

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

This is the code:

import pandas as pd
import numpy as np

df5 = pd.read_csv('MLAN_LLD.txt', skiprows=131,nrows=4, sep='\s{2,}', names=['Interface','Status','Protocol','Description'])

df6 = pd.read_csv('MLAN_LLD.txt', skiprows=138, nrows=3, sep='\s{2,}', names=['Interface','IP-Address','ok','Method','Status','Protocol'])
final_df1 = df5.join(df6, lsuffix='_decription', rsuffix='_ipaddress')
0

There are 0 best solutions below