I have a string:
ONT ONT ONT Auto-neg Speed Duplex Port Flow Native Priority
And i need to get Auto-neg from it (for example).
I have start index of this word, for example 24.
I need to get space count between previous word ONT and Auto-neg
full code:
def _find_all(string: str, finding: str) -> list[int]:
result = []
for i, _ in enumerate(string):
if string[i:i + len(finding)] == finding:
result.append(i)
return result
fields = {}
tables = []
is_table = False
is_table_heading = False
table_heading_raw = ''
is_notes = False
table_fields = []
raw = raw.replace(PAGINATION, '').replace('\x1b[37D', '').replace('x1b[37D', '') # remove stupid pagination
for line in raw.splitlines():
if '#' in line: # prompt lines
continue
if fullmatch(r'\s*\-{5,}\s*', line.strip()): # divider line
is_notes = False
if is_table_heading:
is_table_heading = False
continue
if is_table and not is_table_heading:
is_table = False
continue
if is_table and not is_table_heading: # table field line
assert tables
print(table_fields)
print(split(r'\s+', line))
# tables[-1].append({key: _parse_value(value) for key, value in zip(table_fields, split(r'\s+', line))})
continue
if not is_table and len(split(r'\s+', line)) > 1: # table start heading line
is_table = True
is_table_heading = True
table_heading_raw = line
table_fields = [c for c in split(r'\s+', line.strip()) if c]
tables.append([])
continue
if is_table_heading: # table next heading line
spaces = 0
for i, field in enumerate(table_fields):
print(len(field) + spaces + len(table_heading_raw) - len(table_heading_raw.lstrip()), len(line) - len(line.lstrip()))
if len(field) + spaces + len(table_heading_raw) - len(table_heading_raw.lstrip()) == len(line) - len(line.lstrip()):
table_fields[i] += '-' + split(r'\s+', line.lstrip(), maxsplit=1)[0]
spaces += len((match(r'^\s*\S*', line) or [])[0])
line = split(r'^\s*\S*', line)[-1]
else:
raw_index = _find_all(table_heading_raw, field)[table_fields[:i].count(field)]
# print(table_heading_raw[raw_index])
spaces += len(split(r'\S*', table_heading_raw[_find_all(table_heading_raw, field)[table_fields[:i].count(field)]])[0])
I am trying to make table parser like
----------------------------------------------------------------------------
ONT ONT ONT Auto-neg Speed Duplex Port Flow Native Priority
port port-type (Mbps) switch control VLAN
----------------------------------------------------------------------------
1 2 ETH enable auto auto on off 2 0
----------------------------------------------------------------------------
I have no clue how to parse table title (it can be multiline).
text[:24], remove spaces on its right endrstrip()and get length of both strings and substract them.for-loop from 24 to 0 (to iterate from the end to the beginning) and check char-by-char until you get char different than space.