How to Convert YAML to Dataclasses in Python
In this tutorial, you’ll learn various methods to convert YAML data into Python dataclasses.
We’ll explore both manual methods and multiple popular libraries that simplify this conversion.
Manual parsing
To manually convert YAML data into dataclasses, start by defining your dataclass structure and then parse the YAML content accordingly.
import yaml from dataclasses import dataclass from typing import List yaml_data = """ users: - name: Ahmed age: 30 email: ahmed@example.com - name: Fatma age: 25 email: fatma@example.com """ @dataclass class User: name: str age: int email: str @dataclass class UsersData: users: List[User] data = yaml.safe_load(yaml_data) users = [User(**user) for user in data['users']] users_data = UsersData(users=users) print(users_data)
Output:
UsersData(users=[User(name='Ahmed', age=30, email='ahmed@example.com'), User(name='Fatma', age=25, email='fatma@example.com')])
Using dacite
You can use the dacite
library to simplify the conversion from dictionaries to dataclasses.
import yaml from dataclasses import dataclass from typing import List from dacite import from_dict yaml_data = """ users: - name: Sara age: 27 email: sara@example.com - name: Karim age: 35 email: karim@example.com """ @dataclass class User: name: str age: int email: str @dataclass class UsersData: users: List[User] data = yaml.safe_load(yaml_data) users_data = from_dict(data_class=UsersData, data=data) print(users_data)
Output:
UsersData(users=[User(name='Sara', age=27, email='sara@example.com'), User(name='Karim', age=35, email='karim@example.com')])
Using marshmallow-dataclass
You can use the marshmallow-dataclass
to integrate data validation with dataclass conversion.
import yaml from dataclasses import dataclass from typing import List import marshmallow_dataclass yaml_data = """ users: - name: Mona age: 32 email: mona@example.com - name: Tarek age: 29 email: tarek@example.com """ @dataclass class User: name: str age: int email: str @dataclass class UsersData: users: List[User] UsersSchema = marshmallow_dataclass.class_schema(UsersData) data_dict = yaml.safe_load(yaml_data) users_data = UsersSchema().load(data_dict) print(users_data)
Output:
UsersData(users=[User(name='Mona', age=32, email='mona@example.com'), User(name='Tarek', age=29, email='tarek@example.com')])
Using dataclass-wizard
Implement dataclass-wizard
for easy serialization and deserialization between YAML and dataclasses.
import yaml from dataclass_wizard import YAMLWizard from typing import List from dataclasses import dataclass yaml_data = """ users: - name: Youssef age: 40 email: youssef@example.com - name: Dina age: 26 email: dina@example.com """ @dataclass class User(YAMLWizard): name: str age: int email: str @dataclass class UsersData(YAMLWizard): users: List[User] users_data = UsersData.from_yaml(yaml_data) print(users_data)
Output:
UsersData(users=[User(name='Youssef', age=40, email='youssef@example.com'), User(name='Dina', age=26, email='dina@example.com')])
Mokhtar is the founder of LikeGeeks.com. He is a seasoned technologist and accomplished author, with expertise in Linux system administration and Python development. Since 2010, Mokhtar has built an impressive career, transitioning from system administration to Python development in 2015. His work spans large corporations to freelance clients around the globe. Alongside his technical work, Mokhtar has authored some insightful books in his field. Known for his innovative solutions, meticulous attention to detail, and high-quality work, Mokhtar continually seeks new challenges within the dynamic field of technology.