Run a function over every row of a CSV — with progress, header validation, and structured per-row errors.
Project description
tha-csv-runner
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
- Opens the CSV and validates that all
required_headersare present — raises immediately if any are missing - Iterates every row with a
tqdmprogress bar labelled withdesc - Calls your
validator(row)function — if it raises, that row is marked as an error and processing continues - Appends three columns to every row:
row number,row status, andmessagerow numberstarts at 2 (row 1 is the header)- On success:
row statusandmessageare blank - On error:
row status = "error",message = str(exception)
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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file tha_csv_runner-0.2.6.tar.gz.
File metadata
- Download URL: tha_csv_runner-0.2.6.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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f192839e28e2a348d1a4d2c5c89a5448d13c9de5f98ca6edcd568a3bcce33173
|
|
| MD5 |
8d119711e74a36f7fb8e6ea60620a70d
|
|
| BLAKE2b-256 |
bbb249f7e011ba0e028a85dc4dfd1b4d564bb937b2a205bb2e99e66a94524322
|
Provenance
The following attestation bundles were made for tha_csv_runner-0.2.6.tar.gz:
Publisher:
publish.yml on tha-guy-nate/tha-csv-runner
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
tha_csv_runner-0.2.6.tar.gz -
Subject digest:
f192839e28e2a348d1a4d2c5c89a5448d13c9de5f98ca6edcd568a3bcce33173 - Sigstore transparency entry: 1551910729
- Sigstore integration time:
-
Permalink:
tha-guy-nate/tha-csv-runner@c2612b5a0184a75352a11dcc3b25c77cdb57ef8d -
Branch / Tag:
refs/tags/v0.2.6 - Owner: https://github.com/tha-guy-nate
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@c2612b5a0184a75352a11dcc3b25c77cdb57ef8d -
Trigger Event:
push
-
Statement type:
File details
Details for the file tha_csv_runner-0.2.6-py3-none-any.whl.
File metadata
- Download URL: tha_csv_runner-0.2.6-py3-none-any.whl
- Upload date:
- Size: 6.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ff7fcbe40bc02a2b482878a14e3daf551ff4b1aac9cc8a204b5720a965113e77
|
|
| MD5 |
393796df762a94ae24a99675cd828aaf
|
|
| BLAKE2b-256 |
982bedb4971d1f93bdb87d7ec3c1224fe21a898d21d3be4c40cbf22e6404d641
|
Provenance
The following attestation bundles were made for tha_csv_runner-0.2.6-py3-none-any.whl:
Publisher:
publish.yml on tha-guy-nate/tha-csv-runner
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
tha_csv_runner-0.2.6-py3-none-any.whl -
Subject digest:
ff7fcbe40bc02a2b482878a14e3daf551ff4b1aac9cc8a204b5720a965113e77 - Sigstore transparency entry: 1551910780
- Sigstore integration time:
-
Permalink:
tha-guy-nate/tha-csv-runner@c2612b5a0184a75352a11dcc3b25c77cdb57ef8d -
Branch / Tag:
refs/tags/v0.2.6 - Owner: https://github.com/tha-guy-nate
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@c2612b5a0184a75352a11dcc3b25c77cdb57ef8d -
Trigger Event:
push
-
Statement type: