Leverage the built-in python csv module to write files in jsonl format
Project description
csv-jsonlines
A convenient module for writing a list of dictionaries to a .jsonl
-formatted text file, suitable for ingestion by BigQuery and other services.
csv-jsonlines
is built on top of Python's built-in csv
module. It allows you to specify a fieldnames
list to add a bit of assurance. Otherwise, no schema-handling is offered.
Why not Just Use csv
Files?
If you are here asking that question, I'm guessing you have not spent exciting times attempting to clean up poorly-formatted csv files (I'm looking at you, Excel).
Installation
pip install csv-jsonl
Usage
>>> from csv_jsonl import JSONLinesDictWriter
>>> l = [{"foo": "bar", "bat": 1}, {"foo": "bar", "bat": 2}]
>>> with open("foo.jsonl", "w", encoding="utf-8") as _fh:
... writer = JSONLinesDictWriter(_fh)
... writer.writerows(l)
...
>>> d = {"foo": "bar", "bat": 1}
>>> with open("bar.jsonl", "w", encoding="utf-8") as _fh:
... writer = JSONLinesDictWriter(_fh)
... writer.writerow(d)
...
>>> from collections import OrderedDict
>>> od = OrderedDict([('foo', 'bar'), ('bat', 1)])
>>> with open("qux.jsonl", "w", encoding="utf-8") as _fh:
... writer = JSONLinesDictWriter(_fh)
... writer.writerow(od)
...
>>> fieldnames = ["foo", "bar"] # keys = ["foo", "bat"] expect fail
>>> with open("baz.jsonl", "w", encoding="utf-8") as _fh:
... writer = JSONLinesDictWriter(_fh, fieldnames=fieldnames)
... writer.writerows(l)
...
Expect ValueError
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
csv-jsonl-0.1.3.tar.gz
(16.3 kB
view hashes)
Built Distribution
csv_jsonl-0.1.3-py3-none-any.whl
(16.9 kB
view hashes)
Close
Hashes for csv_jsonl-0.1.3-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | fac219d18b81d1c2d6244e2fd94f7d01b35a9982af529310f3cbfd7441ed6939 |
|
MD5 | 49ee82d3c14eb91d3aa5df4f83e1f356 |
|
BLAKE2b-256 | b919cf4ab6403f844fc6a8e9b7817b79fa3cfc0b21e132220a33ea73fe59ae70 |