Skip to main content

Run a function over every row of a CSV — with progress, header validation, and structured per-row errors.

Project description

tha-csv-runner

CI

A small Python library that runs a function against every row of a CSV — with a progress bar, required header validation, and structured error capture per row.

Install

pip install tha-csv-runner

Quick start

from tha_csv_runner import ThaCSV

def process(row: dict) -> None:
    """Raise any exception to mark the row as an error. Return value is ignored."""
    if not row["email"].endswith("@example.com"):
        raise ValueError("invalid email domain")

runner = ThaCSV()

rows = runner.read("Step 1 of 2", "data.csv", ["name", "email"], process)
runner.write("Step 2 of 2", "output.csv")

How it works

  1. Opens the CSV and validates that all required_headers are present — raises immediately if any are missing
  2. Iterates every row with a tqdm progress bar labelled with desc
  3. Calls your validator(row) function — if it raises, that row is marked as an error and processing continues
  4. Appends three columns to every row: row number, row status, and message
    • row number starts at 2 (row 1 is the header)
    • On success: row status and message are blank
    • On error: row status = "error", message = str(exception)
  5. write() writes all rows (success and error) to a CSV

API

ThaCSV

ThaCSV()

runner.read()

runner.read(
    "Step 1 of 2",           # progress bar label — pass None to use the filename
    "data.csv",              # path to input CSV
    ["a", "b"],              # columns that must exist — raises ConfigError if missing
    validator=my_func,       # optional: callable(row: dict) -> None
    enrich=True,             # optional: set False to skip row number/status/message columns
)

Reads and processes all rows. Returns the rows as a list[dict] (same object as runner.rows).

When enrich=False, validator exceptions are re-raised instead of captured.

runner.write()

runner.write(
    "Step 2 of 2",                     # progress bar label — pass None for "Writing {stem} CSV"
    output_path="output.csv",          # optional — auto-named input_processed_TIMESTAMP.csv if omitted
    rows=my_rows,                      # optional — use these rows instead of runner.rows
    sort_by="name",                    # optional — column name, or list of column names
    ascending=True,                    # optional — bool or list of bools matching sort_by
    column_order=["name", "email"],    # optional — listed columns come first, rest follow
    keep=["name", "email"],            # optional — keep only these columns (mutually exclusive with drop)
    drop=["row number"],               # optional — remove these columns (mutually exclusive with keep)
    chunk_size=1000,                   # optional — split output into files of this many rows
)

Prints :white_check_mark: Done! CSV was written to: {path} on completion. Override by setting runner.status_cb = my_fn.

Returns the Path that was written, or a list[Path] when chunk_size is set.

chunk_size

When provided, write() splits the output into multiple files named output_001.csv, output_002.csv, etc. and returns a list[Path].

paths = runner.write("Step 2 of 2", "output.csv", chunk_size=1000)
# ["output_001.csv", "output_002.csv", ...]

License

MIT

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

tha_csv_runner-0.2.5.tar.gz (33.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

tha_csv_runner-0.2.5-py3-none-any.whl (6.3 kB view details)

Uploaded Python 3

File details

Details for the file tha_csv_runner-0.2.5.tar.gz.

File metadata

  • Download URL: tha_csv_runner-0.2.5.tar.gz
  • Upload date:
  • Size: 33.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for tha_csv_runner-0.2.5.tar.gz
Algorithm Hash digest
SHA256 dfb9ac36bafb8316103c8ab3785b3d4ea0c1db3546928fbed7d8acf967bcf319
MD5 d1a64957251d6f3952f0de3826aae8ee
BLAKE2b-256 edbc955a545ea83e96f34401d80cf39d0c91275aa62ed194e26a9fa473928b99

See more details on using hashes here.

Provenance

The following attestation bundles were made for tha_csv_runner-0.2.5.tar.gz:

Publisher: publish.yml on tha-guy-nate/tha-csv-runner

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file tha_csv_runner-0.2.5-py3-none-any.whl.

File metadata

  • Download URL: tha_csv_runner-0.2.5-py3-none-any.whl
  • Upload date:
  • Size: 6.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for tha_csv_runner-0.2.5-py3-none-any.whl
Algorithm Hash digest
SHA256 a0d0c8243bb607779592f9da2511acbdc532f7767abde17181affa1650a3b042
MD5 59430995aa0c15a2c07147e570cfdd07
BLAKE2b-256 5c65e6af83420c2ea44e18890e24bb3dbe176cc3e4fef2491e6580da08530f98

See more details on using hashes here.

Provenance

The following attestation bundles were made for tha_csv_runner-0.2.5-py3-none-any.whl:

Publisher: publish.yml on tha-guy-nate/tha-csv-runner

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page