PydanticIO (pronounce pidantisio) is a tiny file IO utility library for Python powered by Pydantic. This library is a port of the Rust library SerdeIO
# standard distribution
pip install pydanticio
# with YAML backend
pip install pydanticio[yaml]- CSV by stdlib
csvmodule - JSON by stdlib
jsonmodule - JSON Lines by stdlib
jsonmodule - YAML by
pyyamllibrary, it is an optional feature and you need enable it manually at when you install it.
read_record_from_readeris used to read a typeTwhich is a subclass ofpydantic.BaseModelfromTextIO. Data format must be specified byDataFormatliterals.read_records_from_readeralways tries to deserialize the data aslist[T].read_record_from_fileandread_records_from_fileaccepts aPath. Data format is automatically determined by file extension.write_*functions follow the same rules asread_*.
Note that some data format like CSV and JSON Lines support only reading records list[T].
from pydantic import BaseModel
from pydanticio import read_records_from_file, write_records_to_file
class User(BaseModel):
name: str
age: int
users = read_records_from_file("users.csv", User)
write_records_to_file("users.json", users)