Question:
How to save python yaml and load a nested class?

Solution:

Instead of using ‘pyyaml,’ use 'ruamel.yaml'. 'pyyaml' might not able to handle nested objects as it has a few limitations.


Pip Install:


pip install ruamel.yaml


Code

# hello.py import pydantic as dan class Hello: @dan.dataclasses.dataclass class Data: data_a: int from ruamel import yaml data = Hello.Data(1) filename = "hi.yaml" with open(filename, "w") as fh: yaml.dump(data, fh) with open(filename, "r") as fh: yaml.load(fh, yaml.Loader) # Causes the error.


Suggested blogs: 

>ChatGPT: Features, advantages & disadvantages-ChatGPT

>Python File Error: "Time Limit Exceeded" | Solved-Python

>Know more about ChatGPT: Terminology & Capabilities-ChatGPT

>Build a minimal API using ASP.Net Core with Android Studio Code-Asp.net

>How to route between different components in React.js?-react js


Submit
0 Answers