嗨,在我的代码中有一本字典的字典。在Python中访问字典词典
nrec={'bridge': 'xapi1', 'current_operations': {}, 'uuid': '9ae5ca7d-e7d6-7a81-f619-d0ea33efb534', 'tags': [], 'other_config': {'is_guest_installer_network': 'true', 'netmask': '255.255.255.0', 'ip_end': '192.168.128.254', 'ip_begin': '192.168.128.1'}, 'name_label': 'Guest installer network', 'VIFs': ['OpaqueRef:dff106aa-1a94-8384-1c86-862b47c87fcf'], 'allowed_operations': [], 'PIFs': [], 'name_description': 'Network on which guests will get assigned a private local IP address', 'MTU': '1500', 'blobs': {}}
在这里你可以看到这本词典里面多了一个词典'other_config': {'is_guest_installer_network': 'true', 'netmask': '255.255.255.0', 'ip_end': '192.168.128.254', 'ip_begin': '192.168.128.1'}
在那里。
我要检查is_guest_installer_network=="true"
我已经做了nrec["other_config"]["is_guest_installer_network"]== "true"
但问题是一些属性有要么是空值或不同的值,这other_config属性。那么在这种情况下,我的解决方案会抛出异常所以我想以一种有效的方式来做到这一点,如果is_guest_installer_network包含在字典中,并且值(字符串)为真或不真实。
如果值不存在,你想返回什么:'False'? – moooeeeep 2012-03-29 07:20:52