Python module for parsing semi-structured text into python tables.
npx @tessl/cli install tessl/pypi-textfsm@2.1.0TextFSM is a Python library that implements a template-based state machine for parsing semi-structured text output into structured data. Originally developed at Google for parsing CLI command output from networking devices, it provides a powerful template language for extracting data from human-readable text formats like log files, command outputs, and configuration files.
pip install textfsmimport textfsmFor CLI table functionality:
from textfsm import clitableFor table formatting:
from textfsm import texttableFor terminal utilities:
from textfsm import terminalimport io
import textfsm
# Basic parsing with TextFSM
template_text = """
Value INTERFACE (\S+)
Value IP_ADDRESS (\d+\.\d+\.\d+\.\d+)
Value STATUS (up|down)
Start
^Interface ${INTERFACE} is ${STATUS}, line protocol is ${STATUS}
^ Internet address is ${IP_ADDRESS}/ -> Record
"""
# Parse network device output
input_text = """
Interface GigabitEthernet0/0/0 is up, line protocol is up
Internet address is 192.168.1.1/24
Interface GigabitEthernet0/0/1 is down, line protocol is down
Internet address is 10.0.0.1/30
"""
# Create parser and parse
fsm = textfsm.TextFSM(io.StringIO(template_text))
results = fsm.ParseText(input_text)
print(results)
# [['GigabitEthernet0/0/0', '192.168.1.1', 'up'],
# ['GigabitEthernet0/0/1', '10.0.0.1', 'down']]TextFSM uses a finite state machine approach with four main components:
The library also includes CLI automation utilities (CliTable) for mapping templates to device types, tabular data structures (TextTable), and terminal formatting utilities.
The main TextFSM parsing functionality for processing templates and extracting structured data from text input using finite state machine rules.
class TextFSM:
def __init__(self, template, options_class=None): ...
def ParseText(self, text): ...
def Reset(self): ...
class TextFSMValue:
def __init__(self, options_class=None): ...
def Parse(self, line): ...
class TextFSMRule:
def __init__(self, line, line_num=0, var_map=None, options_class=None): ...Automated template selection and CLI data parsing with index files that map TextFSM templates to device and command combinations.
class CliTable:
def __init__(self, index_file=None, template_dir=None): ...
def ParseCmd(self, cmd_input, attributes=None): ...
def AddKeys(self, keys): ...
class IndexTable:
def __init__(self, file_or_data=None): ...
def GetRowMatch(self, **kwargs): ...Tabular data representation with formatting capabilities for displaying parsed results as structured tables.
class TextTable:
def __init__(self, header=None): ...
def Append(self, row_data): ...
def CsvToTable(self, csv_data): ...
def LabelValueTable(self, data): ...
class Row:
def __init__(self, table, row_data=None): ...ANSI text formatting, terminal control, and paging functionality for enhanced command-line output display.
def AnsiText(text, **kwargs): ...
def StripAnsiText(text): ...
class Pager:
def __init__(self, command=None): ...
def Page(self, text): ...TextFSM provides command-line tools for parsing text files:
# Parse text using a template file
textfsm template.textfsm input.txt
# Display help
textfsm --helpThe CLI interface is available through the main parser module:
def main(argv=None):
"""
Command-line interface for TextFSM parsing.
Args:
argv (list): Command line arguments (default: sys.argv)
Returns:
int: Exit code (0 for success)
"""# Core Parser Exceptions
class Error(Exception):
"""Base exception class for TextFSM."""
class UsageError(Exception):
"""Command line execution error."""
class TextFSMError(Error):
"""Error in FSM state execution."""
class TextFSMTemplateError(Error):
"""Errors while parsing templates."""
class FSMAction(Exception):
"""Base class for FSM action indicators."""
class SkipRecord(FSMAction):
"""Indicates current record should be skipped."""
class SkipValue(FSMAction):
"""Indicates current value should be skipped."""
# CLI Table Exceptions
class IndexTableError(Error):
"""Index table operation error."""
class CliTableError(Error):
"""General CliTable error."""
# Text Table Exceptions
class TableError(Error):
"""Error in TextTable operations."""
# Terminal Exceptions
class TerminalError(Error):
"""Base exception class for terminal module."""
class TerminalUsageError(TerminalError):
"""Command line format error for terminal module."""__version__ = '2.1.0'