Access嵌套Python对象返回None
我正在努力访问下面更新对象的“from”中嵌套的“is_bot”属性:Access嵌套Python对象返回None,python,Python,我正在努力访问下面更新对象的“from”中嵌套的“is_bot”属性: {'message': {'caption_entities': [], 'channel_chat_created': False, 'chat': {'first_name': 'Rodrigo Formighieri', 'id': 446924384, 'type': 'priv
{'message': {'caption_entities': [],
'channel_chat_created': False,
'chat': {'first_name': 'Rodrigo Formighieri',
'id': 446924384,
'type': 'private',
'username': 'rodrigoformi'},
'date': 1516040454,
'delete_chat_photo': False,
'entities': [{'length': 7, 'offset': 0, 'type': 'bot_command'}],
'from': {'first_name': 'Rodrigo Formighieri',
'id': 446924384,
'is_bot': False,
'language_code': 'pt-BR',
'username': 'rodrigoformi'},
'group_chat_created': False,
'message_id': 145,
'new_chat_member': None,
'new_chat_members': [],
'new_chat_photo': [],
'photo': [],
'supergroup_chat_created': False,
'text': '/invite'},
'update_id': 241263441}
我所尝试的:
update.get('message',{}).get('from',{}).is_bot
此up返回AttributeError:“Message”对象没有属性“get”
最后一个返回TypeError:“非类型”对象不可下标
我不知道该怎么办了。有什么帮助吗?试试这个:
d = {'message': {'caption_entities': [],
'channel_chat_created': False,
'chat': {'first_name': 'Rodrigo Formighieri',
'id': 446924384,
'type': 'private',
'username': 'rodrigoformi'},
'date': 1516040454,
'delete_chat_photo': False,
'entities': [{'length': 7, 'offset': 0, 'type': 'bot_command'}],
'from': {'first_name': 'Rodrigo Formighieri',
'id': 446924384,
'is_bot': False,
'language_code': 'pt-BR',
'username': 'rodrigoformi'},
'group_chat_created': False,
'message_id': 145,
'new_chat_member': None,
'new_chat_members': [],
'new_chat_photo': [],
'photo': [],
'supergroup_chat_created': False,
'text': '/invite'},
'update_id': 241263441}
is_bot = d.get('message').get('from').get('is_bot')
print (is_bot) # False
当我或我的学生在努力获得一个深度嵌套的值时,我通常会通过它来确保我得到了,看到了我所期望的。正如一些评论者所指出的,您的第二个版本可以正常工作,所以在原始代码中一定有一些我们看不到的细微差别。下面是您关心的关键点的一步:
In [45]: update = {'message': {'caption_entities': [],
...: 'channel_chat_created': False,
...: 'chat': {'first_name': 'Rodrigo Formighieri',
...: 'id': 446924384,
...: 'type': 'private',
...: 'username': 'rodrigoformi'},
...: 'date': 1516040454,
...: 'delete_chat_photo': False,
...: 'entities': [{'length': 7, 'offset': 0, 'type': 'bot_command'}],
...: 'from': {'first_name': 'Rodrigo Formighieri',
...: 'id': 446924384,
...: 'is_bot': False,
...: 'language_code': 'pt-BR',
...: 'username': 'rodrigoformi'},
...: 'group_chat_created': False,
...: 'message_id': 145,
...: 'new_chat_member': None,
...: 'new_chat_members': [],
...: 'new_chat_photo': [],
...: 'photo': [],
...: 'supergroup_chat_created': False,
...: 'text': '/invite'},
...: 'update_id': 241263441}
...:
In [46]: update['message']
Out[46]:
{'caption_entities': [],
'channel_chat_created': False,
'chat': {'first_name': 'Rodrigo Formighieri',
'id': 446924384,
'type': 'private',
'username': 'rodrigoformi'},
'date': 1516040454,
'delete_chat_photo': False,
'entities': [{'length': 7, 'offset': 0, 'type': 'bot_command'}],
'from': {'first_name': 'Rodrigo Formighieri',
'id': 446924384,
'is_bot': False,
'language_code': 'pt-BR',
'username': 'rodrigoformi'},
'group_chat_created': False,
'message_id': 145,
'new_chat_member': None,
'new_chat_members': [],
'new_chat_photo': [],
'photo': [],
'supergroup_chat_created': False,
'text': '/invite'}
In [47]: update['message']['from']
Out[47]:
{'first_name': 'Rodrigo Formighieri',
'id': 446924384,
'is_bot': False,
'language_code': 'pt-BR',
'username': 'rodrigoformi'}
In [48]: update['message']['from']['is_bot']
Out[48]: False
已解决:
update = ast.literal_eval(str(update))
update["message"]["from"]["is_bot"] #false
必须转换强制转换字符串,然后使用ast.literal评估得到的该死的更新对象。您的第一次尝试失败,因为
is\u bot
不是一个属性,它是一个字典键,所以您使用订阅来获取它(['is\u bot']
)。你的第二次尝试对我没有任何错误。你的第二次尝试对我也很好。一定有什么你没有告诉我们的是错误的。这是一个属性,正如其他人所建议的,这里有一个使用json对象的链接。什么是消息对象?它来自哪里?我的第一个应该是update.get('message',{})。get('from',{})。get('is_bot',“”)然后呢?我的第二个总是返回我显示的错误。这很有效。但我不能在我的代码中这样做。执行ast.literal_eval(更新)时,它返回错误:raise VALUERROR('格式错误的节点或字符串:'+repr(节点))VALUERROR:格式错误的节点或字符串:。有趣的是,当我打印(更新)并从bash中获取字符串,然后使用字符串执行您在答案中所做的操作时,它就工作了。我快疯了!!!代替eval,尝试:dict(您的_字符串)创建一个字典您是如何打印的?没有细微差别。def invite(bot,update):打印(update[“message”][“from”][“is_bot”])这是代码,仍然会出现“NoneType”对象不可订阅”错误。
update = ast.literal_eval(str(update))
update["message"]["from"]["is_bot"] #false