YAML



• Easily convert a YAML file to a Python Object
◇ yaml.load(yaml_data)
◇ yaml.dump(object)
• YAML Objects become Dictionaries
• YAML Lists become Lists

pip install PyYAML



import yaml
from pprint import pprint

yml_example = open("yaml_example.yaml").read()
pprint(yml_example)

# Convert YAML objects to Dictionaries / Lists
yaml_python = yaml.load(yml_example)

int_name = yaml_python["ietf-interfaces:interface"].["name"]
int_name

# Convert Dictionaries / List to YAML
yaml.dump(yaml_python)



• yaml_example.yaml

---
ietf-interfaces:interface:
  nameGigabitEthernet2
  descriptionWide Area Network
  enabledtrue
  ietf-ip:ipv4:
    address:
    - ip172.16.0.2
      netmask255.255.255.0

Index