Structure semi-structured text
Project description
Structures semi-structured text, useful when parsing command line output from networking devices.
What is it
Well that’s where structifytext tries to help. It lets you define the payload you wish came back to you, and with a sprinkle of the right regular expressions it does!
Usage
At less than 100 lines of code it’s quite simple. The parse_struct method expects a “structure” and an output string converted to a list (I found the easiest way to do this is to use StringIO.readlines()).
The Struct
The structure is recursively parsed, to populate the dictionary/structure that was provided with values from the input string list.
An example is useful here.
E.g. The following structure.
{ 'tables': [ { 'id': '\[TABLE (\d{1,2})\]', 'flows': [ { 'id': '\[FLOW_ID(\d+)\]', 'info': 'info\s+=\s+(.*)' } ] } ] }
Will create a “chunk/block” from the following output
[TABLE 0] Total entries: 3 [FLOW_ID1] info = related to table 0 flow 1 [TABLE 1] Total entries: 31 [FLOW_ID1] info = related to table 1 flow 1
That will be parsed as:
{ 'tables': [{ 'id': '0', 'flows': [{ 'id': '1', 'info': 'related to table 0 flow 1' }], }, { 'id': '1', 'flows': [{ 'id': '1', 'info': 'related to table 1 flow 1' }] }] }
See under tests/test_parser_api.py for more usage examples.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for structifytext-0.2.0-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2a5a543f7e25f7109dbaeb78aa46b5e40b99ae77856711d0fcaff2ba3497697f |
|
MD5 | 15d381b6bb1f7948fac2e2793de3d6ab |
|
BLAKE2b-256 | 3b6e80b5b3351424d34dc212002ee18c540750a04e2e9b672897f0ec40213f90 |