How do you load a multi-document Kubernetes YAML file?
IntermediateConceptData formats, HTTP & REST APIs
Answer
import yaml
with open("manifests.yaml", encoding="utf-8") as f:
docs = [d for d in yaml.safe_load_all(f) if d] # skip empty docs between ---
for d in docs:
print(d["kind"], d["metadata"]["name"])
yaml.safe_load on a multi-document file raises expected a single document in the stream. Watch for YAML 1.1 surprises in PyYAML: unquoted on, yes, no become booleans and 010 can become octal, which is why we quote values in manifests. ruamel.yaml preserves comments and ordering when you need to edit files round-trip.
More on Data formats, HTTP & REST APIs
How do you read and write JSON, and what are the common gotchas?BeginnerWhy must you use `yaml.safe_load` rather than `yaml.load`?BeginnerHow do you read and write CSV files properly?BeginnerHow do you make an HTTP request with `requests` the right way?BeginnerWhy use a `requests.Session`?IntermediateHow do you add retries with exponential backoff to `requests`?Intermediate