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

CSV Refine generates an HTML report summarizing the overall quality of the dataset and the errors detected during processing.

CSV Refine HTML 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.1 is available on PyPI.

Metadata

Release files for csv-refine 0.1.1

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

Built distribution (wheel)

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

Total release size: 55.0 kB

Release files / csv_refine-0.1.1.tar.gz

Download URL csv_refine-0.1.1.tar.gz
Size 31.4 kB
Tags Source
SHA-256 checksum
How to use checksums
ca2955fe706c5f4b53eb1f27a25210c67959920060f0f21788aa49e539795876
BLAKE2b-256 checksum
How to use checksums
965dfc8ea4e32c5f53c452d1595c49b37bd84555687a81a93f34af5a5b07e248
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.1-py3-none-any.whl

Download URL csv_refine-0.1.1-py3-none-any.whl
Size 23.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
16e7baded93b142539f5e93c0089ace0c556d91e60ce3faa932227dee873a6a6
BLAKE2b-256 checksum
How to use checksums
2d85ab78a8eb1cfc7083663daa2c7c312ce9013982de9c85d11a598c5988b017
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

0.1.2

2 release files

This release

0.1.1 This release

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