我想打印类型为String
的entity_id
和attribute
的值,例如
entities = [{'id': 'Room1',
'temperature': {'value': '10', 'type': 'String'},
'pressure': {'value': '12', 'type': 'Number'},
'type': 'Room',
'time_index': '2020-08-24T06:23:37.346348'},
{'id': 'Room2',
'temperature': {'value': '10', 'type': 'Number'},
'pressure': {'value': '12', 'type': 'Number'},
'type': 'Room',
'time_index': '2020-08-24T06:23:37.346664'}]
ngsi_type = ( 'Array', 'Boolean')
这是代码的实际定义,我想在其中打印id and attribute
的值,即temperature , pressure
对于type
它不是ngsi_type
,例如,对于type : String
,我想打印它的entity_id and attribute
的值
def _insert_entities_of_type(self,
entity_type,
entities,
fiware_service=None,
fiware_servicepath=None):
for e in entities:
if e[NGSI_TYPE] != entity_type:
msg = "Entity {} is not of type {}."
raise ValueError(msg.format(e[NGSI_ID], entity_type))
关于它的任何帮助,因为我是python新手,不能用msg打印除ngsi类型之外的id and attribute:temperature , pressure
值
您可以使用
isinstance(obj, type)
检查属性本身是否是dict-如果是,您需要“下降”到其中。你可以这样压扁你的口述:输出:
您在列表上的循环是正确的,但是您需要循环到每个字典中查找您的类型
要迭代每个的键值对,请执行以下操作:
然后要检查嵌套字典的类型,首先要键入check,确保键存在,最后确保它不是NSGI_类型。所有这些看起来像:
相关问题 更多 >
编程相关推荐