2021-04-09 15:10:38 -07:00
|
|
|
[Home](https://kellyjonbrazil.github.io/jc/)
|
2022-01-25 17:07:47 -08:00
|
|
|
<a id="jc.parsers.kv"></a>
|
2020-07-30 16:20:24 -07:00
|
|
|
|
|
|
|
# jc.parsers.kv
|
2022-01-25 17:07:47 -08:00
|
|
|
|
2022-12-27 14:01:03 -08:00
|
|
|
jc - JSON Convert `Key/Value` file and string parser
|
2020-08-05 16:51:58 -07:00
|
|
|
|
2022-04-04 11:42:16 -07:00
|
|
|
Supports files containing simple key/value pairs.
|
|
|
|
|
|
|
|
- Delimiter can be `=` or `:`. Missing values are supported.
|
|
|
|
- Comment prefix can be `#` or `;`. Comments must be on their own line.
|
|
|
|
- If duplicate keys are found, only the last value will be used.
|
2020-08-05 16:51:58 -07:00
|
|
|
|
2022-04-04 12:00:41 -07:00
|
|
|
> Note: Values starting and ending with quotation marks will have the marks
|
2022-05-26 15:07:54 -07:00
|
|
|
> removed. If you would like to keep the quotation marks, use the `-r`
|
|
|
|
> command-line argument or the `raw=True` argument in `parse()`.
|
2020-08-05 16:51:58 -07:00
|
|
|
|
2020-08-05 13:32:59 -07:00
|
|
|
Usage (cli):
|
2020-07-30 16:20:24 -07:00
|
|
|
|
2022-01-25 18:03:34 -08:00
|
|
|
$ cat foo.txt | jc --kv
|
2020-07-30 16:20:24 -07:00
|
|
|
|
2020-08-05 13:32:59 -07:00
|
|
|
Usage (module):
|
|
|
|
|
2022-01-25 18:03:34 -08:00
|
|
|
import jc
|
|
|
|
result = jc.parse('kv', kv_file_output)
|
2022-01-18 15:38:03 -08:00
|
|
|
|
2021-04-09 15:10:38 -07:00
|
|
|
Schema:
|
|
|
|
|
2023-01-03 13:04:13 -08:00
|
|
|
Key/Value document converted to a dictionary - see the python configparser
|
|
|
|
standard library documentation for more details.
|
2022-01-25 18:03:34 -08:00
|
|
|
|
|
|
|
{
|
|
|
|
"key1": string,
|
|
|
|
"key2": string
|
|
|
|
}
|
|
|
|
|
|
|
|
Examples:
|
|
|
|
|
|
|
|
$ cat keyvalue.txt
|
|
|
|
# this file contains key/value pairs
|
|
|
|
name = John Doe
|
|
|
|
address=555 California Drive
|
|
|
|
age: 34
|
2023-01-08 10:55:48 -08:00
|
|
|
|
2022-01-25 18:03:34 -08:00
|
|
|
; comments can include # or ;
|
|
|
|
# delimiter can be = or :
|
|
|
|
# quoted values have quotation marks stripped by default
|
|
|
|
# but can be preserved with the -r argument
|
|
|
|
occupation:"Engineer"
|
|
|
|
|
|
|
|
$ cat keyvalue.txt | jc --kv -p
|
|
|
|
{
|
|
|
|
"name": "John Doe",
|
|
|
|
"address": "555 California Drive",
|
|
|
|
"age": "34",
|
|
|
|
"occupation": "Engineer"
|
|
|
|
}
|
2022-01-25 17:07:47 -08:00
|
|
|
|
|
|
|
<a id="jc.parsers.kv.parse"></a>
|
|
|
|
|
2022-03-05 12:15:14 -08:00
|
|
|
### parse
|
2022-01-25 17:07:47 -08:00
|
|
|
|
2020-07-30 16:20:24 -07:00
|
|
|
```python
|
2022-01-25 17:07:47 -08:00
|
|
|
def parse(data, raw=False, quiet=False)
|
2020-07-30 16:20:24 -07:00
|
|
|
```
|
|
|
|
|
|
|
|
Main text parsing function
|
|
|
|
|
2022-01-25 18:03:34 -08:00
|
|
|
Parameters:
|
2020-07-30 16:20:24 -07:00
|
|
|
|
2022-01-25 18:03:34 -08:00
|
|
|
data: (string) text data to parse
|
|
|
|
raw: (boolean) unprocessed output if True
|
|
|
|
quiet: (boolean) suppress warning messages if True
|
2020-07-30 16:20:24 -07:00
|
|
|
|
2022-01-25 18:03:34 -08:00
|
|
|
Returns:
|
2020-07-30 16:20:24 -07:00
|
|
|
|
2023-01-03 12:55:38 -08:00
|
|
|
Dictionary representing a Key/Value pair document.
|
2020-07-30 16:20:24 -07:00
|
|
|
|
2022-01-25 19:18:54 -08:00
|
|
|
### Parser Information
|
2021-04-09 15:10:38 -07:00
|
|
|
Compatibility: linux, darwin, cygwin, win32, aix, freebsd
|
|
|
|
|
2023-12-21 14:55:21 -08:00
|
|
|
Source: [`jc/parsers/kv.py`](https://github.com/kellyjonbrazil/jc/blob/master/jc/parsers/kv.py)
|
|
|
|
|
2024-02-26 20:18:57 -08:00
|
|
|
Version 2.2 by Kelly Brazil (kellyjonbrazil@gmail.com)
|