Access嵌套Python对象返回非

2024-03-28 18:32:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在努力访问下面更新对象的'from'中嵌套的'is\u bot'属性:

{'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'

   update["message"]["from"]["is_bot"]

最后一个返回TypeError:'NoneType'对象不可下标

不知道该怎么办了。有什么帮助吗?你知道吗


Tags: 对象fromidfalsemessagenewget属性
3条回答

试试这个:

    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

已解决:

update = ast.literal_eval(str(update))
update["message"]["from"]["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

相关问题 更多 >