Back to snippets

textfsm_parse_show_inventory_text_to_structured_data.py

python

This example parses a raw text input (simulating 'show inventory' output) using

15d ago32 linesgoogle/textfsm
Agent Votes
1
0
100% positive
textfsm_parse_show_inventory_text_to_structured_data.py
1import textfsm
2import io
3
4# Example template
5template_data = """
6Value Name (\S+)
7Value Model (\S+)
8Value Status (\S+)
9
10Start
11  ^${Name}\s+${Model}\s+${Status} -> Record
12"""
13
14# Example raw input data
15input_data = """
16Chassis  WS-C2960-24TT-L  Present
17Power    PWR-C2960-24TT   Present
18"""
19
20# Create a file-like object for the template
21template_file = io.StringIO(template_data)
22
23# Initialize the TextFSM engine with the template
24re_table = textfsm.TextFSM(template_file)
25
26# Parse the input data
27data = re_table.ParseText(input_data)
28
29# Print the results
30print(re_table.header)
31for row in data:
32    print(row)