Skip to main content

csv-jsonl

A convenient module for writing a list of dictionaries or list of lists to a .jsonl-formatted text file, suitable for ingestion by BigQuery and other services.

csv-jsonl 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).

Other Data Formats

Basically supports anything with a __getitem__, as well as dataclasses. See test for everything.

Installation

pip install csv-jsonl

Usage

List of Dictonaries

>>> 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

List of Lists

        >>> 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)
        ...

pipeline status Latest Release Downloads

Metadata

Release files for csv-jsonl 0.1.6

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for csv-jsonl 0.1.6
File Size Uploaded Provenance
csv-jsonl-0.1.6.tar.gz 17.2 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for csv-jsonl 0.1.6
File Interpreter ABI Platform
csv_jsonl-0.1.6-py3-none-any.whl Python 3 none any Details

Total release size: 35.5 kB

Release files / csv-jsonl-0.1.6.tar.gz

Download URL csv-jsonl-0.1.6.tar.gz
Size 17.2 kB
Tags Source
SHA-256 checksum
How to use checksums
f270e5babf7f8e42804de30289f1fed7cb1f3d2b70899b6e2ecf814a26eb3b98
BLAKE2b-256 checksum
How to use checksums
a17aeada95f82b5f627d77c8b4f4f34ec66e4d21c3c52587908eb5c2fddaaa72
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.6

Release files / csv_jsonl-0.1.6-py3-none-any.whl

Download URL csv_jsonl-0.1.6-py3-none-any.whl
Size 18.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a1f0113ca916cbb60775bea2ebd31fec94dcf5c4794811189cff0cce7ed280a2
BLAKE2b-256 checksum
How to use checksums
a434c51199c7c5d2243bccfd4ddf9be898c049e6a453705dc851d73cca68d6a2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.6

Security for csv-jsonl 0.1.6

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page