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

104 lines
2.7 KiB
Markdown
Raw Normal View History

2021-10-24 13:10:47 -07:00
[Home](https://kellyjonbrazil.github.io/jc/)
# jc.parsers.csv_s
jc - JSON CLI output utility `csv` file streaming parser
2021-10-30 13:57:36 -07:00
> This streaming parser outputs JSON Lines
2022-01-19 17:30:14 -08:00
The `csv` streaming parser will attempt to automatically detect the
delimiter character. If the delimiter cannot be detected it will default
to comma. The first row of the file must be a header row.
2021-10-24 13:10:47 -07:00
2022-01-19 17:30:14 -08:00
Note: The first 100 rows are read into memory to enable delimiter detection,
then the rest of the rows are loaded lazily.
2021-10-24 13:10:47 -07:00
Usage (cli):
$ cat file.csv | jc --csv-s
Usage (module):
2022-01-18 14:18:12 -08:00
import jc
2022-01-19 17:30:14 -08:00
# result is an iterable object (generator)
result = jc.parse('csv_s', csv_output.splitlines())
2022-01-18 14:18:12 -08:00
for item in result:
# do something
or
2021-10-24 13:10:47 -07:00
import jc.parsers.csv_s
2022-01-19 17:30:14 -08:00
# result is an iterable object (generator)
result = jc.parsers.csv_s.parse(csv_output.splitlines())
2021-11-22 09:10:11 -08:00
for item in result:
# do something
2021-10-24 13:10:47 -07:00
Schema:
2022-01-19 17:30:14 -08:00
csv file converted to a Dictionary:
https://docs.python.org/3/library/csv.html
2021-10-24 13:10:47 -07:00
2021-10-30 13:47:21 -07:00
{
"column_name1": string,
2021-11-22 09:36:58 -08:00
"column_name2": string,
2022-01-19 17:30:14 -08:00
# below object only exists if using -qq or ignore_exceptions=True
"_jc_meta":
2021-11-22 09:36:58 -08:00
{
2022-01-19 17:30:14 -08:00
"success": boolean, # false if error parsing
2021-12-09 10:58:06 -08:00
"error": string, # exists if "success" is false
"line": string # exists if "success" is false
2021-11-22 09:36:58 -08:00
}
2021-10-30 13:47:21 -07:00
}
2021-10-24 13:10:47 -07:00
Examples:
$ cat homes.csv
2022-01-19 17:30:14 -08:00
"Sell", "List", "Living", "Rooms", "Beds", "Baths", "Age", "Acres"...
2021-10-24 13:10:47 -07:00
142, 160, 28, 10, 5, 3, 60, 0.28, 3167
175, 180, 18, 8, 4, 1, 12, 0.43, 4033
129, 132, 13, 6, 3, 1, 41, 0.33, 1471
...
$ cat homes.csv | jc --csv-s
2022-01-19 17:30:14 -08:00
{"Sell":"142","List":"160","Living":"28","Rooms":"10","Beds":"5"...}
{"Sell":"175","List":"180","Living":"18","Rooms":"8","Beds":"4"...}
{"Sell":"129","List":"132","Living":"13","Rooms":"6","Beds":"3"...}
2021-10-24 13:10:47 -07:00
...
## info
```python
info()
```
Provides parser metadata (version, author, etc.)
## parse
```python
parse(data, raw=False, quiet=False, ignore_exceptions=False)
```
Main text parsing generator function. Returns an iterator object.
Parameters:
2022-01-19 17:30:14 -08:00
data: (iterable) line-based text data to parse
(e.g. sys.stdin or str.splitlines())
2021-10-24 13:10:47 -07:00
raw: (boolean) output preprocessed JSON if True
quiet: (boolean) suppress warning messages if True
ignore_exceptions: (boolean) ignore parsing exceptions if True
Yields:
Dictionary. Raw or processed structured data.
Returns:
Iterator object
## Parser Information
Compatibility: linux, darwin, cygwin, win32, aix, freebsd
2022-01-03 09:19:40 -08:00
Version 1.2 by Kelly Brazil (kellyjonbrazil@gmail.com)