I have some code that generates a nested dictionary structure. I'm then printing it as YAML by doing the following:
print(yaml.dump(mydict, sort_keys=False))
the data structure looks something like the following
"DeviceInfo": {
"autoApply": 'true',
"comment": "this is a test",
"contact": {
"email": "Hello",
"name": "aaaaa",
"phoneNumber": "(111) 111 111"
},
"location": {
"address": "2100 Moorpark Ave",
"city": "San Jose",
"country": "US",
"latitude": 36,
"longitude": 119,
"state": "California",
"zipCode": 94088
},
"networkRole": "non-hub",
"region": "North",
"group": "Group 1",
}}
and the resulting YAML:
DeviceInfo:
autoApply: 'true'
comment: this is a test
contact:
email: Hello
name: aaaaa
phoneNumber: (111) 111 111
location:
address: 2100 Moorpark Ave
city: San Jose
country: US
latitude: 36
longitude: 119
state: California
zipCode: 94088
networkRole: non-hub
region: North
group: Group 1
what I would like to do is order the dictionary and therefore resulting YAML by key depth.
So have the output look like this.
DeviceInfo:
autoApply: 'true'
comment: this is a test
networkRole: non-hub
region: North
group: Group 1
contact:
email: Hello
name: aaaaa
phoneNumber: (111) 111 111
location:
address: 2100 Moorpark Ave
city: San Jose
country: US
latitude: 36
longitude: 119
state: California
zipCode: 94088
From testing yaml.dump ( with sort_keys=False ) seems to preserve the order of the dictionary. So what I am trying to accomplish is to sort the dictionary by ascending depth.