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

runner.read("Step 1 of 1", "data.csv", ["name", "email"], process)
runner.write("Step 1 of 1", "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 processor(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
    • 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 2 of 10",          # 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
    processor=my_func,       # optional: callable(row: dict) -> None
    enrich=True,             # optional: set False to skip row number/status/message columns
)

Reads and processes all rows. Results are stored in runner.rows as a list of dicts.

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

runner.write()

runner.write(
    "Step 10 of 10",                   # progress bar label — pass None to use the output filename
    output_path="output.csv",          # optional — auto-named input_processed_TIMESTAMP.csv if omitted
    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)
)

Returns the Path that was written.

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.1.tar.gz (32.0 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.1-py3-none-any.whl (5.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: tha_csv_runner-0.2.1.tar.gz
  • Upload date:
  • Size: 32.0 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.1.tar.gz
Algorithm Hash digest
SHA256 bb1ab73dc539281f178dd33dca532b09e3fe79d6c5ee931c0b213c5346e0674f
MD5 c3819b5680f67e17458a34673a7f0011
BLAKE2b-256 15edd9294b2f133a9a9f1f769117782de8d8cbd56ec7b0f21d7a349dd109ed80

See more details on using hashes here.

Provenance

The following attestation bundles were made for tha_csv_runner-0.2.1.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.1-py3-none-any.whl.

File metadata

  • Download URL: tha_csv_runner-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 5.6 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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 27fd811b6e140825e6ce64888a97ce8301a5301092f916ca066fd193383534ac
MD5 bd1932d9a60bb6480ed749afb0cef407
BLAKE2b-256 cc8f4fbb046b2b74614f7083b525a8db852245ab2d89ae6e1eb47bf6868c6c0b

See more details on using hashes here.

Provenance

The following attestation bundles were made for tha_csv_runner-0.2.1-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