1
0
mirror of https://github.com/kellyjonbrazil/jc.git synced 2025-06-17 00:07:37 +02:00
Files
jc/docs/parsers/asciitable.md

150 lines
4.0 KiB
Markdown
Raw Normal View History

2022-03-22 12:25:59 -07:00
[Home](https://kellyjonbrazil.github.io/jc/)
<a id="jc.parsers.asciitable"></a>
# jc.parsers.asciitable
jc - JSON Convert `asciitable` parser
This parser converts ASCII and Unicode text tables with single-line rows.
Column headers must be at least two spaces apart from each other and must
2022-05-26 11:37:16 -07:00
be unique. For best results, column headers should be left-justified. If
column separators are present, then non-left-justified headers will be fixed
automatically.
Row separators are optional and are ignored. Each non-row-separator line is
considered a separate row in the table.
2022-03-22 12:25:59 -07:00
For example:
╒══════════╤═════════╤════════╕
│ foo │ bar │ baz │
╞══════════╪═════════╪════════╡
│ good day │ │ 12345 │
├──────────┼─────────┼────────┤
│ hi there │ abc def │ 3.14 │
╘══════════╧═════════╧════════╛
2022-03-29 09:35:54 -07:00
or
2022-03-22 12:25:59 -07:00
+-----------------------------+
| foo bar baz |
+-----------------------------+
| good day 12345 |
| hi there abc def 3.14 |
+-----------------------------+
2022-03-29 09:35:54 -07:00
or
2022-03-22 12:25:59 -07:00
| foo | bar | baz |
|----------|---------|--------|
| good day | | 12345 |
| hi there | abc def | 3.14 |
2022-03-29 09:35:54 -07:00
or
2022-03-22 12:25:59 -07:00
foo bar baz
--------- -------- ------
good day 12345
2022-03-22 12:42:07 -07:00
hi there abc def 3.14
2022-03-22 12:25:59 -07:00
2022-03-29 09:58:44 -07:00
or
2022-03-24 16:58:45 -07:00
foo bar baz
good day 12345
hi there abc def 3.14
2022-03-29 09:35:54 -07:00
etc...
2022-03-22 12:25:59 -07:00
2022-03-29 09:35:54 -07:00
Headers (keys) are converted to snake-case. All values are returned as
strings, except empty strings, which are converted to None/null.
2022-03-22 13:21:10 -07:00
2022-06-15 11:25:21 -07:00
> Note: To preserve the case of the keys use the `-r` cli option or
> `raw=True` argument in `parse()`.
2022-03-22 12:25:59 -07:00
Usage (cli):
$ cat table.txt | jc --asciitable
Usage (module):
import jc
result = jc.parse('asciitable', asciitable_string)
Schema:
[
{
"column_name1": string, # empty string is null
"column_name2": string # empty string is null
}
]
Examples:
2022-03-22 12:35:56 -07:00
$ echo '
> ╒══════════╤═════════╤════════╕
> │ foo │ bar │ baz │
> ╞══════════╪═════════╪════════╡
> │ good day │ │ 12345 │
> ├──────────┼─────────┼────────┤
> │ hi there │ abc def │ 3.14 │
> ╘══════════╧═════════╧════════╛' | jc --asciitable -p
[
{
"foo": "good day",
"bar": null,
"baz": "12345"
},
{
"foo": "hi there",
"bar": "abc def",
"baz": "3.14"
}
]
$ echo '
> foo bar baz
> --------- -------- ------
> good day 12345
2022-03-22 12:42:07 -07:00
> hi there abc def 3.14' | jc --asciitable -p
2022-03-22 12:35:56 -07:00
[
{
"foo": "good day",
"bar": null,
"baz": "12345"
},
{
"foo": "hi there",
"bar": "abc def",
2022-03-22 12:42:07 -07:00
"baz": "3.14"
2022-03-22 12:35:56 -07:00
}
]
2022-03-22 12:25:59 -07:00
<a id="jc.parsers.asciitable.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
2023-12-21 14:55:21 -08:00
Source: [`jc/parsers/asciitable.py`](https://github.com/kellyjonbrazil/jc/blob/master/jc/parsers/asciitable.py)
2022-06-15 11:25:21 -07:00
Version 1.2 by Kelly Brazil (kellyjonbrazil@gmail.com)