Skip to main content

CSV Dataclass Generator

Generate Python dataclasses and loader functions from CSV/TSV files.

Features

  • Automatic Type Inference: Detects int, float, and str types based on a sample of rows.
  • Dialect Detection: Automatically identifies CSV delimiters (including TSV).
  • Name Sanitization: Converts CSV column headers into valid Python identifiers.
  • Iterator Loading: Generates a loading generator that yields dataclass instances.

Installation

pip install csv-dataclass-gen

Usage

Command Line Interface (CLI)

The package provides a csv-dataclass-gen command.

# Generate code to stdout
csv-dataclass-gen data.csv

# Generate code and save to a directory
csv-dataclass-gen data.csv --output ./generated_models/

# Specify a custom class name and sample size for type inference
csv-dataclass-gen data.csv --name my_custom_data --sample-size 500

CLI Help Message:

Usage: csv-dataclass-gen [OPTIONS] INPUT_FILE

  Generate dataclass and loader code from CSV files.

Options:
  -o, --output TEXT          Output directory for generated files. "-" outputs
                             the result to stdout.
  -s, --sample-size INTEGER  Number of rows to sample for type inference
  -n, --name TEXT            Alternative name for the generated name. Snake
                             case / spaced words is recommended.
  --help                     Show this message and exit.

Arguments:

  • INPUT_FILE: Path to the CSV/TSV file (required).

Example Generated Code

Given a CSV like users.csv:

id,user_name,score
1,alice,95.5
2,bob,88.0

The generator will produce:

from dataclasses import dataclass
from pathlib import Path
from typing import Iterator
import csv

@dataclass
class Users:
    id: int  # Original: "id"
    user_name: str  # Original: "user_name"
    score: float  # Original: "score"

def load_users(csv_path: Path, max_rows: int | None = None, delimiter: str = ',') -> Iterator[Users]:
    # ... loading logic ...
    pass

Development

Code Template

The generator comes with a Dataclass and Loader template.

Dependencies

This project uses uv for dependency management.

uv sync --all-groups

Running Tests

We use pytest for testing.

uv run pytest

Tests include:

  • tests/test_name_sanitizer.py: Logic for sanitizing names into different formats.
  • tests/test_type_inferrer.py: Logic for detecting data types.
  • tests/test_csv_analyzer.py: Logic for CSV structure analysis.
  • tests/test_code_gen_e2e.py: End-to-end tests that generate code and verify it by reconstructing the original CSV.

License

MIT License – see the LICENSE file for details.

Metadata

Release files for csv-dataclass-gen 1.0.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-dataclass-gen 1.0.1
File Size Uploaded
csv_dataclass_gen-1.0.1.tar.gz 31.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for csv-dataclass-gen 1.0.1
File Interpreter ABI Platform
csv_dataclass_gen-1.0.1-py3-none-any.whl Python 3 none any Details

Total release size: 42.3 kB

Release files / csv_dataclass_gen-1.0.1.tar.gz

Download URL csv_dataclass_gen-1.0.1.tar.gz
Size 31.6 kB
Tags Source
SHA-256 checksum
How to use checksums
9656fa09301a9db1f3456c4b93cdcece52d40ca9eebf332ec7e9c11af2c5a405
BLAKE2b-256 checksum
How to use checksums
bf07205ba7e46f8ec986f946b30b94e440ce7b478ea0da1691f60f10758fb218
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Apr 24, 2026.

Transparency log

Release files / csv_dataclass_gen-1.0.1-py3-none-any.whl

Download URL csv_dataclass_gen-1.0.1-py3-none-any.whl
Size 10.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
4c03dcd17d287b12e432a99f9efdb3793c6fdafd06174cf9867c928009621e79
BLAKE2b-256 checksum
How to use checksums
c9e1d12975422660f99e2073146f2808588d5ac71000e4d8e3d41cf5ef0bf42c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Apr 24, 2026.

Transparency log

Release history Release notifications | RSS feed

This release

1.0.1 This release

2 release files

1.0.0

2 release files

0.1.3

2 release files

0.1.2

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