boo*_*oog 4 python json list python-requests
我正在使用 python requests 库从 api 获取数据。数据以名为 messages 的大型 json 数组形式返回。其中包含许多单独的“消息”子级 json(请参阅底部的 json 响应示例)。
在此 json 响应中,对于每条消息,我只关心几个(2 或 3)个数据点。我需要获取这几个数据点,并将它们存储到某个东西(列表的列表、字典等)中,以便我稍后可以引用它,存储到一个对象并从另一个函数中使用。
我需要存储的数据点是id
、conversationId
和body. The
id is unique, while the
conventionId` 在对话中的所有消息之间共享,因此不是唯一的。
以下是我尝试过的一些事情,只是为了大致了解如何做到这一点:
response=requests.get(url + id, headers=h, params=p)
messages=json.loads(response.text)
for message in messages:
print(message['body'])
Run Code Online (Sandbox Code Playgroud)
^^ 这里我只是想看看是否可以引用特定消息的正文,但没有成功。
r=requests.get(url + id, headers=h, params=p)
inbound_dict = {}
inbound=json.loads(r.text)
for item in inbound['messages']:
inbound_dict[item['conversationId']] = item['body']
print(inbound_dict)
Run Code Online (Sandbox Code Playgroud)
^^ 这个实际上确实有点工作,但不允许我有效地组织数据以便稍后调用。当我打印字典时,它会显示最新的值,因为键不是唯一的......所以它是覆盖而不是附加。这就是我认为列表列表最好的原因。
最后,我想要一个解决方案,其中数据通过conversationId
字典或类似结构进行组织,我可以通过conversationId
、 或引用消息msgId
,以及一种干净且易于阅读的方式来存储所有数据...:)
response=requests.get(url + id, headers=h, params=p)
messages=json.loads(response.text)
for message in messages:
print(message['body'])
Run Code Online (Sandbox Code Playgroud)
最后,这是一个 json 示例。请记住,我仍在学习并跟上 python 的速度。感谢大家抽出宝贵的时间!
b'{"id":1005672,"messages":[{"id":4461048,"body":"Mnow test test","conversationId":1005672,"locationId":2045,"contactId":12792806,"assignedUserId":0,"status":"RECEIVED","error":null,"kind":"INCOMING","outgoing":false,"reviewRequest":false,"type":"SMS","readDate":0,"respondedDate":0,"sentDate":1576783232355,"attachments":[]},{"id":4461049,"body":"THIS NUMBER DOES NOT CURRENTLY ACCEPT TEXT MESSAGES PLEASE CALL (716) 444-4444 TO WORK WITH ONE OF OUR INTAKE SPECIALISTS","conversationId":1005672,"locationId":2045,"contactId":12792806,"assignedUserId":0,"status":"RECEIVED","error":null,"kind":"AUTO_RESPONSE","outgoing":true,"reviewRequest":false,"type":"SMS","readDate":0,"respondedDate":0,"sentDate":1576783233546,"attachments":[]},{"id":4620511,"body":"test sms,test sms","conversationId":1005672,"locationId":2045,"contactId":12792806,"assignedUserId":17297,"status":"DELIVERED","error":null,"kind":"API","outgoing":true,"reviewRequest":false,"type":"SMS","readDate":0,"respondedDate":0,"sentDate":1577987093930,"attachments":[]},...
Run Code Online (Sandbox Code Playgroud)
您可以使用列表理解和字典理解的组合来做到这一点,如下所示:
import json
from pprint import pprint
response = b'{"id":1005672,"messages":[{"id":4461048,"body":"Mnow test test","conversationId":1005672,"locationId":2045,"contactId":12792806,"assignedUserId":0,"status":"RECEIVED","error":null,"kind":"INCOMING","outgoing":false,"reviewRequest":false,"type":"SMS","readDate":0,"respondedDate":0,"sentDate":1576783232355,"attachments":[]},{"id":4461049,"body":"THIS NUMBER DOES NOT CURRENTLY ACCEPT TEXT MESSAGES PLEASE CALL (716) 444-4444 TO WORK WITH ONE OF OUR INTAKE SPECIALISTS","conversationId":1005672,"locationId":2045,"contactId":12792806,"assignedUserId":0,"status":"RECEIVED","error":null,"kind":"AUTO_RESPONSE","outgoing":true,"reviewRequest":false,"type":"SMS","readDate":0,"respondedDate":0,"sentDate":1576783233546,"attachments":[]},{"id":4620511,"body":"test sms,test sms","conversationId":1005672,"locationId":2045,"contactId":12792806,"assignedUserId":17297,"status":"DELIVERED","error":null,"kind":"API","outgoing":true,"reviewRequest":false,"type":"SMS","readDate":0,"respondedDate":0,"sentDate":1577987093930,"attachments":[]}]}'
data = json.loads(response)
messages = [
{'id': message['id'],
'conversationId': message['conversationId'],
'body': message['body']} for message in data['messages']
]
pprint(messages, sort_dicts=False)
Run Code Online (Sandbox Code Playgroud)
输出:
import json
from pprint import pprint
response = b'{"id":1005672,"messages":[{"id":4461048,"body":"Mnow test test","conversationId":1005672,"locationId":2045,"contactId":12792806,"assignedUserId":0,"status":"RECEIVED","error":null,"kind":"INCOMING","outgoing":false,"reviewRequest":false,"type":"SMS","readDate":0,"respondedDate":0,"sentDate":1576783232355,"attachments":[]},{"id":4461049,"body":"THIS NUMBER DOES NOT CURRENTLY ACCEPT TEXT MESSAGES PLEASE CALL (716) 444-4444 TO WORK WITH ONE OF OUR INTAKE SPECIALISTS","conversationId":1005672,"locationId":2045,"contactId":12792806,"assignedUserId":0,"status":"RECEIVED","error":null,"kind":"AUTO_RESPONSE","outgoing":true,"reviewRequest":false,"type":"SMS","readDate":0,"respondedDate":0,"sentDate":1576783233546,"attachments":[]},{"id":4620511,"body":"test sms,test sms","conversationId":1005672,"locationId":2045,"contactId":12792806,"assignedUserId":17297,"status":"DELIVERED","error":null,"kind":"API","outgoing":true,"reviewRequest":false,"type":"SMS","readDate":0,"respondedDate":0,"sentDate":1577987093930,"attachments":[]}]}'
data = json.loads(response)
messages = [
{'id': message['id'],
'conversationId': message['conversationId'],
'body': message['body']} for message in data['messages']
]
pprint(messages, sort_dicts=False)
Run Code Online (Sandbox Code Playgroud)
您可以使处理更加数据驱动,并消除推导式中的大量重复编码,从而通过这样做使其更加简洁:
import json
from pprint import pprint
data_points = 'id', 'conversationId', 'body'
response = b'{"id":1005672,"messages":[{"id":4461048,"body":"Mnow test test","conversationId":1005672,"locationId":2045,"contactId":12792806,"assignedUserId":0,"status":"RECEIVED","error":null,"kind":"INCOMING","outgoing":false,"reviewRequest":false,"type":"SMS","readDate":0,"respondedDate":0,"sentDate":1576783232355,"attachments":[]},{"id":4461049,"body":"THIS NUMBER DOES NOT CURRENTLY ACCEPT TEXT MESSAGES PLEASE CALL (716) 444-4444 TO WORK WITH ONE OF OUR INTAKE SPECIALISTS","conversationId":1005672,"locationId":2045,"contactId":12792806,"assignedUserId":0,"status":"RECEIVED","error":null,"kind":"AUTO_RESPONSE","outgoing":true,"reviewRequest":false,"type":"SMS","readDate":0,"respondedDate":0,"sentDate":1576783233546,"attachments":[]},{"id":4620511,"body":"test sms,test sms","conversationId":1005672,"locationId":2045,"contactId":12792806,"assignedUserId":17297,"status":"DELIVERED","error":null,"kind":"API","outgoing":true,"reviewRequest":false,"type":"SMS","readDate":0,"respondedDate":0,"sentDate":1577987093930,"attachments":[]}]}'
data = json.loads(response)
messages = [{dp: message.get(dp) for dp in data_points}
for message in data['messages']]
pprint(messages, sort_dicts=False)
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
14592 次 |
最近记录: |