python yaml - ghdrako/doc_snipets GitHub Wiki
             serialize list to yaml using dump
import yaml
users = [{'name': 'John Doe', 'occupation': 'gardener'},
         {'name': 'Lucy Black', 'occupation': 'teacher'}]
print(yaml.dump(users))
- name: John Doe
  occupation: gardener
- name: Lucy Black
  occupation: teacher
serialize to file
#!/usr/bin/env python3
import yaml
users = [{'name': 'John Doe', 'occupation': 'gardener'},
         {'name': 'Lucy Black', 'occupation': 'teacher'}]
with open('users.yaml', 'w') as f:
    
    data = yaml.dump(users, f)
Read yaml
#!/usr/bin/env python3
import yaml
with open('items.yaml') as f:
    
    data = yaml.load(f, Loader=yaml.FullLoader) # The FullLoader parameter handles the conversion from YAML
                                                # scalar values to Python the dictionary format
                                                # the same using full_load()
                                                # yaml.full_load(file)
    print(data)
    sorted = yaml.dump(data, sort_keys=True)
    print(sorted)
#!/usr/bin/env python3
import yaml
with open('items.yaml') as f:
    
    data = yaml.scan(f, Loader=yaml.FullLoader)
    for token in data:
        print(token)