I have a table like output and I would like to get the input from each line separated by column.
Unfortunately the column does not always have the same width and there can be whitespaces inside the column.
But the column always starts at the position of the description in the headline and ends 1 field before the next headline.
My idea was to get the position of the words in the headline and then split the line with something like
var = line[firstword:secondword-1]
var2 = line[secodword:thirdword-1]
Port Name Status Vlan Duplex Speed Type
Eth1/1 Server1 connected 1 full 10G 10Gbase-SR
Eth1/2 Server 2 notconnec 1234 full 10G SFP-1000BAS
Eth1/3 That is poss err-disab trunk full 10G 10Gbase-SR
If you know the column headers, you can do something like this (assuming variable
lines contains the table:
indices = [lines.index(header) for header in ["Port", "Name", "Status", "Vlan", "Duplex", "Speed", "Type"]]
If you don't, then assuming the headers don't contain whitespace (similarly to as Karmanya Aggarwal already suggested):
indices = [lines.index(header) for header in [w for w in lines.split(" ") if w != ""]]
Or using a regex:
indices = [lines.index(header) for header in [w for w in re.split(r"\s+", lines)]]
Now you can get the column values using the indices:
lines[indices:indices].strip() # -> Server1
Of course, if the table is actually tab-separated, you'll want to use the csv module instead.