Parsing a YAML Configuration File in Python - Handling Multi-Document Files with PyYAML
After trying multiple solutions online, I still can't figure this out... I'm optimizing some code but I'm trying to debug After trying multiple solutions online, I still can't figure this out..... I'm trying to parse a multi-document YAML file using PyYAML in Python 3.9. The file consists of multiple documents separated by `---`, and each document has its own structure. My goal is to read each document into a separate dictionary but I'm working with issues with how to handle this correctly. Here's a snippet of my YAML file: ```yaml # config.yaml --- database: host: localhost port: 5432 --- api: version: v1 enabled: true --- logging: level: DEBUG ``` I've tried using the `yaml.safe_load_all` method, but it seems like I'm only getting the first document parsed. My current code looks like this: ```python import yaml with open('config.yaml', 'r') as f: docs = yaml.safe_load_all(f) config = list(docs) print(config) ``` When I run this, the output is: ``` [{'database': {'host': 'localhost', 'port': 5432}}] ``` It seems to be ignoring the other documents. I expected it to return a list of dictionaries, each representing a different section of the YAML file. I've also checked if the file is properly formatted, and it seems correct. Is there something I'm missing in how I'm handling the generator returned by `safe_load_all`? Should I be iterating over it instead? Any guidance would be appreciated! My development environment is Ubuntu. What's the best practice here? The project is a REST API built with Python. Could someone point me to the right documentation? The stack includes Python and several other technologies. Hoping someone can shed some light on this. Any feedback is welcome! I'm developing on Windows 11 with Python. Thanks for any help you can provide! I'm using Python 3.9 in this project.