yaml_dict = yaml.load(yaml_sample,
Loader=yaml.FullLoader)
The variable yaml_dict is now a dictionary object
containing your YAML file. Had this key/value been a
YAML list, it would have created a list instead, like this:
Click here to view code image
>>> type(yaml_dict)
<class 'dict'>
>>> yaml_dict
{'interface': {'name': 'GigabitEthernet2',
'description': 'Wide
Area Network', 'enabled': True, 'ipv4':
{'address': [{'ip':
'192.168.0.2', 'netmask': '255.255.255.0'}]}}}
As before, you can modify this object to your liking. For
example, you can change the interface name to
GigabitEtherenet1, as shown in Example 5-7.
Example 5-7 Changing an Interface Name Within the
Python Dictionary and Printing the Results
Click here to view code image
>>> yaml_dict["interface"]["name"] =
"GigabitEthernet1"
>>> print(yaml.dump(yaml_dict,
default_flow_style=False))
interface:
description: Wide Area Network
enabled: true
ipv4:
address:
- ip: 192.168.0.2
netmask: 255.255.255.0
name: GigabitEthernet1