dataclass-csv is a Python library designed to effortlessly map CSV data into Python dataclasses. It handles type conversions automatically for standard types and supports custom converters for more complex scenarios. The library provides both a reader and a writer for CSV operations with dataclasses. It is currently at version 1.4.1 and sees active development with a moderate release cadence, addressing issues and adding features.
pip install dataclass-csvVerified import paths — ran on the pinned version, not inferred.
Demonstrates how to define a dataclass, prepare CSV data, and use `DataclassReader` to parse the CSV into a list of dataclass objects, handling automatic type conversion for int, float, and bool.
Review your CSV data for non-standard boolean strings (other than 'true', '1', 'yes', 'y', 'on' for True; or 'false', '0', 'no', 'n', 'off' for False). If your application relied on `ValueError` being raised for invalid boolean values, you'll need to add custom validation or a custom converter to replicate that behavior.
Always ensure your CSV files have unique column headers. If you encounter `DuplicatedHeaderError`, rename duplicate columns in your CSV, or use `dataclasses.field(metadata={'dataclass_csv': {'column_name': '...'}})` for explicit mapping if you absolutely need to handle ambiguous columns (though this is not recommended).For versions older than 1.4.0, use a custom `TypeConverter` to handle date/datetime fields. For versions 1.4.0+, ensure your date strings are in standard formats parseable by Python's `datetime` module, or specify a format string via `dataclasses.field(metadata={'dataclass_csv': {'date_format': '%Y-%m-%d'}})`.For new development environments or contributions, consult the latest `pyproject.toml` and README on GitHub for the recommended dependency management tool (currently `poetry`). If you're only using the library as a dependency, this change does not affect your application.
Modify your CSV file to ensure all header names are unique. If you have columns with logically similar data, give them distinct names (e.g., `value_1`, `value_2`).
Inspect the CSV data and the dataclass field type. Ensure the data matches the type, or provide a custom `TypeConverter` (via `DataclassReader(..., converter=...)`) if you need special handling for conversions or errors.
Verify that your CSV header names exactly match the field names in your dataclass (case-sensitive). If the names differ, use `dataclasses.field(metadata={'dataclass_csv': {'column_name': 'ActualCsvHeader'}})` to explicitly map them.No dependency data recorded yet.