我從電報 api 得到的物件是這樣的:
{
'ok': True,
'result': [
{
'update_id': 633425135,
'message':
{
'message_id': 2,
'from':
{
'id': 5060166011,
'is_bot': False,
'first_name': 'Mosquito',
'username': 'TheMosquitoo',
'language_code': 'en'
},
'chat':
{
'id': 5060166011,
'first_name': 'Mosquito',
'username': 'TheMosquitoo',
'type': 'private'
},
'date': 1641723768,
'text': 'asdfasdf'
}
},
{
'update_id': 633425136,
'message':
{
'message_id': 4,
'from':
{
'id': 874434712,
'is_bot': False,
'first_name': '1DS18TE030',
'username': 'thekumaraswamy',
'language_code': 'en'
},
'chat':
{
'id': 874434712,
'first_name': '1DS18TE030',
'username': 'thekumaraswamy',
'type': 'private'
},
'date': 1641723894,
'text': '/start',
'entities': [
{
'offset': 0,
'length': 6,
'type': 'bot_command'
}]
}
},
{
'update_id': 633425137,
'message':
{
'message_id': 5,
'from':
{
'id': 874434712,
'is_bot': False,
'first_name': '1DS18TE030',
'username': 'thekumaraswamy',
'language_code': 'en'
},
'chat':
{
'id': 874434712,
'first_name': '1DS18TE030',
'username': 'thekumaraswamy',
'type': 'private'
},
'date': 1641723910,
'text': '??'
}
},
{
'update_id': 633425138,
'message':
{
'message_id': 7,
'from':
{
'id': 874434712,
'is_bot': False,
'first_name': '1DS18TE030',
'username': 'thekumaraswamy',
'language_code': 'en'
},
'chat':
{
'id': 874434712,
'first_name': '1DS18TE030',
'username': 'thekumaraswamy',
'type': 'private'
},
'date': 1641723957,
'text': '*kumaraswamy'
}
}]
}
我想映射這個物件,這樣我只能得到訊息,名字和姓氏......請幫助我。
uj5u.com熱心網友回復:
試試這個(obj是你的物件,即 API 呼叫回傳的物件):
result = [
{'message_text': msg['message']['text'],
'username': msg['message']['from']['username'],
'first_name': msg['message']['from']['first_name']}
for msg in obj['result']]
這是結果:
[
{
'message_text': 'asdfasdf',
'username': 'TheMosquitoo',
'first_name': 'Mosquito',
},
{
'message_text': '/start',
'username': 'thekumaraswamy',
'first_name': '1DS18TE030',
},
{
'message_text': '??',
'username': 'thekumaraswamy',
'first_name': '1DS18TE030',
},
{
'message_text': '*kumaraswamy',
'username': 'thekumaraswamy',
'first_name': '1DS18TE030',
},
]
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/408064.html
標籤:
