The easiest way to parse your CSV files
Project description
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
dataclass
mechanism. - 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.
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
CSVLine
andCSVColumns
. It works similarly todataclass.field
but provides additional parameters for parsing and serializing CSV data. It also accepts all the parameters thatfield
accepts. - dump: Serialize data to a CSV formatted file. The data can be a list of
CSVLine
objects or a singleCSVColumns
object. - load: Load data from a CSV formatted file into a list of
CSVLine
objects or a singleCSVColumns
object, depending on the provided dataclass type. - dumps and loads: Same as
dump
andload
but 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.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file classy_csv-0.1.1.tar.gz
.
File metadata
- Download URL: classy_csv-0.1.1.tar.gz
- Upload date:
- Size: 6.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.3 CPython/3.11.9 Linux/6.8.0-31-generic
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4978d2e29f871c4b008288cda1274dd190af8cf8dfae90a4d9aca23a5333fb5a |
|
MD5 | 5272346c60169527829e8045621f7044 |
|
BLAKE2b-256 | b2a66c82a6cd09fe6aa6bb760b62fe69b8020e78368572100da8944383ea2e01 |
File details
Details for the file classy_csv-0.1.1-py3-none-any.whl
.
File metadata
- Download URL: classy_csv-0.1.1-py3-none-any.whl
- Upload date:
- Size: 6.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.3 CPython/3.11.9 Linux/6.8.0-31-generic
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4327c50e502bf4a655b99fbe6e0b1fbfb7e0e05b99f02a1264af040fee330f75 |
|
MD5 | 545b46bb789be01257d6469f1ee060a0 |
|
BLAKE2b-256 | 73a0cdb0cbf291eac0c68bad53f0b71d8903c66b3a910eb5c05846e0cde03621 |