1

I have the following string header (template):

Port          Name               Status    Vlan      Duplex  Speed   Type

and the string str:

Eth1/2        trunk to dg-qwu-29 connected trunk     full    1000    1/10g

Using the header, how can I strip str to the following list ?

[Eth1/2, trunk to dg-qwu-29, connected, trunk, full, 1000, 1/10g]
JahMyst
  • 1,616
  • 3
  • 20
  • 39

2 Answers2

1

The following assumes that the rows and headers follow a whitespace mask. That is, the header text are aligned with the row columns.

import re
header =  "Port          Name               Status    Vlan      Duplex  Speed   Type"
row    =  "Eth1/2        trunk to dg-qwu-29 connected trunk     full    1000    1/10g"
# retrieve indices where each header title begins and ends
matches = [(m.group(0), (m.start(), m.end()-1)) for m in re.finditer(r'\S+', header)]
b,c=zip(*matches)
# each text in the row begins in each header title index and ends at most before the index 
# of the next header title. strip() to remove extra spaces
items = [(row[j[0]:(c[i+1][0] if i < len(c)-1 else len(row))]).strip() for i,j in enumerate(c)]
print items

The above outputs:

['Eth1/2', 'trunk to dg-qwu-29', 'connected', 'trunk', 'full', '1000', '1/10g']

Edit: Index retrieval from https://stackoverflow.com/a/13734572/1847471

Community
  • 1
  • 1
MervS
  • 5,724
  • 3
  • 23
  • 37
0

You didn't provide info about how the column values are formatted, i.e. separator, escape characters and string quotation. Based on your example, I would say that the tricky part is the name column which you will have to extract going per exclusion. This is a quick way to do that, you can start from there:

# Get the first element
first = str.split()[0]
# Get the last part of the string, excluding name 
last = str.split()[::-1][0:5]
last = last[::-1]
# Get the name column via exclusion of the two parts previously calculated
middle = str.split(first)[1].split(last[0])[0].strip()
r_tmp = [first, middle]
result = r_tmp + last
print result
gl051
  • 571
  • 4
  • 8