csv-jsonl¶
Add DictWriter
style class using bits of the built-in csv
module to
handle writing jsonl
files as defined at jsonlines.org.
- class csv_jsonl.JSONLinesDictWriter(_fh, fieldnames=None, extrasaction='raise')¶
Write
json
formatted dictionaries, separated by newlines.Example
>>> 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
- class csv_jsonl.JSONLinesListWriter(_fh, fieldnames=None, extrasaction='raise')¶
Write a list of lists. Optionally supply header.
Example
>>> from csv_jsonl import JSONLineslistWriter >>> l = zip(["foo", "bar", "bat"], range(3), range(3)) >>> with open("foo.jsonl", "w", encoding="utf-8") as _fh: ... writer = JSONLinesListWriter(_fh) ... writer.writerows(l) ... >>> l = zip(["foo", "bar", "bat"], range(3), range(3)) >>> with open("bar.jsonl", "w", encoding="utf-8") as _fh: ... writer = JSONLinesDictWriter(_fh) ... writer.writerow(next(l)) ... >>> fieldnames = ["baz", "qux", "quux"] >>> l = zip(["foo", "bar", "bat"], range(3), range(3)) >>> with open("foo.jsonl", "w", encoding="utf-8") as _fh: ... writer = JSONLinesListWriter(_fh, fieldnames=fieldnames) ... writer.writeheader() ... writer.writerows(l) ...
- writeheader()¶
Write fieldnames as first list
- class csv_jsonl.JSONLinesWriter(_fh: TextIO)¶
Dump json-encoded dictionaries to file in
jsonl
format.- writerow(rowdict: Mapping[str, Union[str, int, float, bool]])¶
Write a dictionary as a
utf-8
encoded string. Test for encoding.The
jsonlines
format allows for anythingjson
will allow, so we comply.- Parameters:
rowdict (Mapping[str, Any]) – dictionary with string values, su
- writerows(row_iterable: Iterable[Mapping[str, Any]])¶
Write an iterable of dictionary-like objects (with
__getitem__
) as\n
-separatedjson
objects.- Parameters:
row_iterable (Iterable[Mapping[str, Any]]) – List of dictionaries.