2022-03-22 07:05:14 -07:00
|
|
|
[Home](https://kellyjonbrazil.github.io/jc/)
|
|
|
|
<a id="jc.parsers.asciitable_m"></a>
|
|
|
|
|
|
|
|
# jc.parsers.asciitable\_m
|
|
|
|
|
|
|
|
jc - JSON Convert `asciitable-m` parser
|
|
|
|
|
|
|
|
This parser converts various styles of ASCII and Unicode text tables with
|
|
|
|
multi-line rows. Tables must have a header row and separator line between
|
|
|
|
rows.
|
|
|
|
|
|
|
|
For example:
|
|
|
|
|
|
|
|
╒══════════╤═════════╤════════╕
|
|
|
|
│ foo │ bar baz │ fiz │
|
|
|
|
│ │ │ buz │
|
|
|
|
╞══════════╪═════════╪════════╡
|
|
|
|
│ good day │ 12345 │ │
|
|
|
|
│ mate │ │ │
|
|
|
|
├──────────┼─────────┼────────┤
|
|
|
|
│ hi there │ abc def │ 3.14 │
|
|
|
|
│ │ │ │
|
|
|
|
╘══════════╧═════════╧════════╛
|
|
|
|
|
2022-03-22 07:21:19 -07:00
|
|
|
Cells with multiple lines within rows will be joined with a newline
|
2022-03-30 14:39:56 -07:00
|
|
|
character ('\\n').
|
2022-03-22 07:05:14 -07:00
|
|
|
|
2022-03-22 07:21:19 -07:00
|
|
|
Headers (keys) are converted to snake-case and newlines between multi-line
|
2022-03-22 13:21:10 -07:00
|
|
|
headers are joined with an underscore. All values are returned as strings,
|
|
|
|
except empty strings, which are converted to None/null.
|
2022-03-22 07:05:14 -07:00
|
|
|
|
|
|
|
Usage (cli):
|
|
|
|
|
|
|
|
$ cat table.txt | jc --asciitable-m
|
|
|
|
|
|
|
|
Usage (module):
|
|
|
|
|
|
|
|
import jc
|
|
|
|
result = jc.parse('asciitable_m', asciitable-string)
|
|
|
|
|
|
|
|
Schema:
|
|
|
|
|
|
|
|
[
|
|
|
|
{
|
2022-03-22 12:25:59 -07:00
|
|
|
"column_name1": string, # empty string is null
|
|
|
|
"column_name2": string # empty string is null
|
2022-03-22 07:05:14 -07:00
|
|
|
}
|
|
|
|
]
|
|
|
|
|
|
|
|
Examples:
|
|
|
|
|
|
|
|
$ echo '
|
|
|
|
> +----------+---------+--------+
|
|
|
|
> | foo | bar | baz |
|
|
|
|
> | | | buz |
|
|
|
|
> +==========+=========+========+
|
|
|
|
> | good day | 12345 | |
|
|
|
|
> | mate | | |
|
|
|
|
> +----------+---------+--------+
|
|
|
|
> | hi there | abc def | 3.14 |
|
|
|
|
> | | | |
|
|
|
|
> +==========+=========+========+' | jc --asciitable-m -p
|
|
|
|
[
|
|
|
|
{
|
2022-03-30 14:39:56 -07:00
|
|
|
"foo": "good day\\nmate",
|
2022-03-22 07:05:14 -07:00
|
|
|
"bar": "12345",
|
2022-03-22 12:35:56 -07:00
|
|
|
"baz_buz": null
|
2022-03-22 07:05:14 -07:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"foo": "hi there",
|
|
|
|
"bar": "abc def",
|
|
|
|
"baz_buz": "3.14"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
|
|
|
$ echo '
|
|
|
|
> ╒══════════╤═════════╤════════╕
|
|
|
|
> │ foo │ bar │ baz │
|
|
|
|
> │ │ │ buz │
|
|
|
|
> ╞══════════╪═════════╪════════╡
|
|
|
|
> │ good day │ 12345 │ │
|
|
|
|
> │ mate │ │ │
|
|
|
|
> ├──────────┼─────────┼────────┤
|
|
|
|
> │ hi there │ abc def │ 3.14 │
|
|
|
|
> │ │ │ │
|
|
|
|
> ╘══════════╧═════════╧════════╛' | jc --asciitable-m -p
|
|
|
|
[
|
|
|
|
{
|
2022-03-30 14:39:56 -07:00
|
|
|
"foo": "good day\\nmate",
|
2022-03-22 07:05:14 -07:00
|
|
|
"bar": "12345",
|
2022-03-22 12:35:56 -07:00
|
|
|
"baz_buz": null
|
2022-03-22 07:05:14 -07:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"foo": "hi there",
|
|
|
|
"bar": "abc def",
|
|
|
|
"baz_buz": "3.14"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
|
|
|
<a id="jc.parsers.asciitable_m.parse"></a>
|
|
|
|
|
|
|
|
### parse
|
|
|
|
|
|
|
|
```python
|
|
|
|
def parse(data: str, raw: bool = False, quiet: bool = False) -> List[Dict]
|
|
|
|
```
|
|
|
|
|
|
|
|
Main text parsing function
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
|
|
|
|
data: (string) text data to parse
|
|
|
|
raw: (boolean) unprocessed output if True
|
|
|
|
quiet: (boolean) suppress warning messages if True
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
List of Dictionaries. Raw or processed structured data.
|
|
|
|
|
|
|
|
### Parser Information
|
|
|
|
Compatibility: linux, darwin, cygwin, win32, aix, freebsd
|
|
|
|
|
|
|
|
Version 1.0 by Kelly Brazil (kellyjonbrazil@gmail.com)
|