【发布时间】:2016-04-20 06:37:54
【问题描述】:
我有以下 python 字典:
'load_balancers': {'App': {'DJMatcher': {'security_group': 'sg-618d1c05', 'service_name': 'djmatcher/svc', 'certificateId': 'net', 'health_check_path': '/djmatcherstatus/ma.html', 'DNS': {'fde': {'record_name': 'platform-enrichment-djmatcher', 'ttl': 60}}}}}
现在它基本上代表以下YAML:
LoadBalancers:
App:
DJMatcher:
certificateId: 'net'
health_check_path: /djmatcherstatus/ma.html
security_group: *svc_sg1
service_name: *sn
DNS:
fde:
record_name: platform-enrichment-djmatcher
ttl: 60
我想删除第二级键 - “App”并保持其余部分不变,这意味着生成的 python 字典应该成为我删除键 App 的位置,但该值现在成为其父键的值“负载平衡器”:
'load_balancers': {'DJMatcher': {'security_group': 'sg-618d1c05', 'service_name': 'djmatcher/svc', 'certificateId': 'net', 'health_check_path': '/djmatcherstatus/ma.html', 'DNS': {'fde': {'record_name': 'platform-enrichment-djmatcher', 'ttl': 60}}}}
有什么好的方法可以实现吗?
【问题讨论】:
-
d['load_balancers'] = d['load_balancers']['App']?
标签: python json python-2.7 python-3.x dictionary