Далее предполагается, что строки и заголовки следуют пробельные маски. То есть текст заголовка выравнивается со столбцами строк.
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
Приведенные выше результаты:
['Eth1/2', 'trunk to dg-qwu-29', 'connected', 'trunk', 'full', '1000', '1/10g']
Редактировать: поиск Указатель от https://stackoverflow.com/a/13734572/1847471
что вы имеете в виду * стрип ул *? – styvane
полоса в смысле функции Python. В настоящее время str.strip() даст [Eth1/2, trunk, to, dg-qwu-29, connected, trunk, full, 1000, 1/10g], но это не соответствует моему шаблону. –
Что дает '.split ('\ t')' give? –