Skip to main content

CSV Refine

CSV Refine is a Python CLI for cleaning, filtering, and normalizing CSV files.

It lets you define exactly how each column should be validated, cleaned, and normalized through a YAML contract. CSV Refine then separates valid and invalid rows and generates reports to assess the overall health of the dataset.

Features

  • Clean and normalize messy CSV data
  • Filter out invalid rows while preserving valid data
  • Detect missing values, duplicates, invalid formats, and rule violations
  • Separate valid and invalid rows into dedicated CSV files
  • Generate JSON and HTML reports to assess CSV data quality

Installation

Install CSV Refine from PyPI:

pip install csv-refine

Check the CLI:

csv-refine --help

Quick start

csv-refine \
  --contract examples/01-input/example-contract.yaml \
  --csv examples/01-input/example.csv \
  --mode permissive

By default, generated files are written to output/.

A custom output directory can be provided with --output.

YAML contract

CSV Refine is configured through a YAML contract that defines exactly how each column should be validated and normalized.

A contract can define data types, nullability, uniqueness, validation rules, transformations, delimiter, and encoding.

Example:

columns:
  Transaction ID:
    type: str
    nullable: false
    unique: true
    rules:
      starts_with: "TXN_"

For the complete contract syntax and all supported options, see the contract README.

Validation modes

Permissive

Invalid rows are collected while processing continues.

Strict

Structural, type conversion, nullability, and uniqueness failures can stop processing.

Validation-rule violations still act as row filters.

Outputs

CSV Refine generates:

output/
├── validated_example.csv
├── errors_example.csv
├── example.json
└── example.html
  • Validated CSV — valid rows with transformations applied
  • Error CSV — invalid rows with their detected errors
  • JSON report — machine-readable summary
  • HTML report — human-readable overview of CSV health

The original CSV is never modified.

Real-world example

The repository includes a 200-row retail dataset containing missing values, invalid categories, malformed identifiers, duplicate values, invalid dates, and conversion errors.

examples/
├── 01-input/
│   ├── example.csv
│   └── example-contract.yaml
└── 02-expected-output/
    ├── validated_example.csv
    ├── errors_example.csv
    ├── example.json
    └── example.html

Run the example with:

csv-refine \
  --contract examples/01-input/example-contract.yaml \
  --csv examples/01-input/example.csv \
  --mode permissive

Example result:

Total rows:    200
Valid rows:    133
Invalid rows:   67
Total errors:  117

Preview

Raw CSV

The original dataset contains missing values, invalid formats, duplicates, and values that do not comply with the configured data contract.

Raw CSV input

Validated rows

Rows that successfully pass the contract validation are cleaned, normalized, and written to a dedicated CSV file.

Validated CSV output

Invalid rows and detected errors

Invalid rows are separated from the validated dataset and include the errors detected during processing.

CSV errors output

Data quality report

Development

Clone the repository:

git clone https://github.com/alexiskuypers/csv-refine.git
cd csv-refine

python3 -m venv .venv
source .venv/bin/activate

pip install -e ".[dev]"

Tests

Run the test suite with:

python3 -m pytest

Stack

Python 3.12+, PyYAML, email-validator, pytest, argparse, pathlib, logging.

Status

Version 0.1.2 is available on PyPI.

Metadata

Release files for csv-refine 0.1.2

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-refine 0.1.2
File Size Uploaded
csv_refine-0.1.2.tar.gz 31.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for csv-refine 0.1.2
File Interpreter ABI Platform
csv_refine-0.1.2-py3-none-any.whl Python 3 none any Details

Total release size: 55.0 kB

Release files / csv_refine-0.1.2.tar.gz

Download URL csv_refine-0.1.2.tar.gz
Size 31.4 kB
Tags Source
SHA-256 checksum
How to use checksums
aa80bb7287b9590ad41ebbccebca2266b3c32e5f934017c63164bd8bfa3fd846
BLAKE2b-256 checksum
How to use checksums
4f2e3c1579278319ed87b27ca8263f69608261421f8acb31cfaad3c68f106691
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.3

Release files / csv_refine-0.1.2-py3-none-any.whl

Download URL csv_refine-0.1.2-py3-none-any.whl
Size 23.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
bd23da544c5c73418a3f1ee13abb10b67d029e58902174702f62b4551db6a85f
BLAKE2b-256 checksum
How to use checksums
5ce79a32ea6eae5597fa3afc31257978f7c464d7e69d5ba6167ec85c8315587e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.3

Release history Release notifications | RSS feed

0.1.3

2 release files

This release

0.1.2 This release

2 release files

0.1.1

2 release files

0.1.0

2 release files

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