Interview Prep/Python for DevOps/Data formats, HTTP & REST APIs

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.

All Python for DevOps questions Roadmap: Python for automation

More on Data formats, HTTP & REST APIs