Classy CSV
Classy CSV is the easiest way to parse your CSV files; you are just one
dataclass away from your data.
The main use case for Classy CSV is to serialize and deserialize tabular data produced internally by applications you are in control of. By creating a dataclass that describes the structure of your CSV data, you can seamlessly convert between CSV files and Python objects.
Data will be read in a struct-of-arrays or array-of-structs manner depending on your data representation. See the API section for more details.
Goals and Non-Goals
Goals
- Ease of Use: The primary goal of Classy CSV is to provide a simple and
intuitive way to parse and serialize CSV files using Python's built-in
dataclassmechanism. - Type Safety and Compatibility: Classy CSV is designed to play nicely with type hints, linters, and language server protocols such as pyright and mypy. This ensures that the code you write is type-safe and can be checked with standard Python tooling.
- Zero Dependencies: Zero worries about dependency issues.
Non-Goals
- Performance: Classy CSV is not optimized for high performance. It is intended to be used with small CSV files.
- Error Handling: Classy CSV is not designed to deal gracefully with malformed CSV files. It assumes that the input CSV files are well-formed.
Installation
You can install Class CSV using pip:
pip install classy-csv
, alternatively you can just copy classy_csv/classy_csv.py into your project
and start using it now!
API
Classy CSV exposes the following functions:
- CSVLine: A base dataclass for representing a single line in a CSV file. Each attribute of the dataclass corresponds to a column in the CSV file. Optional parsers and serializers can be defined for each attribute.
- CSVColumns: A base dataclass for representing columns in a CSV file as lists. Each attribute of the dataclass corresponds to a column in the CSV file and must be a list. Optional parsers and serializers can be defined for each attribute. Loaded data using a column format based on this class will be returned in a struct-of-arrays style.
- csvfield: A helper function to add parser and serializer configurations
to fields in
CSVLineandCSVColumns. It works similarly todataclass.fieldbut provides additional parameters for parsing and serializing CSV data. It also accepts all the parameters thatfieldaccepts. - dump: Serialize data to a CSV formatted file. The data can be a list of
CSVLineobjects or a singleCSVColumnsobject. - load: Load data from a CSV formatted file into a list of
CSVLineobjects or a singleCSVColumnsobject, depending on the provided dataclass type. - dumps and loads: Same as
dumpandloadbut instead of writing into a file handle or reading from it they produce and load from string representations.
Example
from pathlib import Path
import dataclasses as dc
from classy_csv import CSVLine, CSVColumns, csvfield, dump, load, loads, dumps
@dc.dataclass
class CustomerData(CSVLine):
name: str
age: int = csvfield(parser=int)
height_m: float = csvfield(parser=float, serializer=lambda x: f"{x:.2f}")
weight_kg: float = csvfield(parser=float)
rows = [
CustomerData(name="Jane", age=42, height_m=1.65, weight_kg=58),
CustomerData(name="Joe", age=36, height_m=1.75, weight_kg=75),
]
# It's important to open files with `newline=""`!
with Path("./example.csv").open(mode="w", newline="") as csvfile:
dump(csvfile, rows)
with Path("./example.csv").open(mode="r", newline="") as csvfile:
loaded_rows = load(csvfile, CustomerData)
print(loaded_rows)
# Output: [
# CustomerData(name='Jane', age=42, height_m=1.65, weight_kg=58),
# CustomerData(name='Joe', age=36, height_m=1.75, weight_kg=75),
# ]
More examples can be found under the examples/ folder.
Metadata
Release files for classy-csv 0.1.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| classy_csv-0.1.2.tar.gz | 7.5 kB | Without provenance | Details for classy_csv-0.1.2.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| classy_csv-0.1.2-py3-none-any.whl | Python 3 | none | any | Details for classy_csv-0.1.2-py3-none-any.whl |
Total release size: 14.9 kB
Release files / classy_csv-0.1.2.tar.gz
| Download URL | classy_csv-0.1.2.tar.gz |
|---|---|
| Size | 7.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
8bcd8fab52490cfd0c68d796ad1ffb3985e100710bf69acaa4d15ba8dd2b6d27
|
|
BLAKE2b-256 checksum How to use checksums |
04bf0232b7fddd23748e1ce41d74b9982df44e65a789de3703038e1ba220a162
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.8.3 CPython/3.11.9 Linux/6.8.0-31-generic
|
Release files / classy_csv-0.1.2-py3-none-any.whl
| Download URL | classy_csv-0.1.2-py3-none-any.whl |
|---|---|
| Size | 7.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
8a71bf9ac5055480b30785c8e4ee78180066076567fea1ef1cb96c6312bd03e0
|
|
BLAKE2b-256 checksum How to use checksums |
a8c04cb6b7e758f82e3b625b1ae0c2cb8be38df8c4feee52db7c90fc100ddab4
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.8.3 CPython/3.11.9 Linux/6.8.0-31-generic
|
Security for classy-csv 0.1.2
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.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.