如何使用python从JSON提取特定字段和值? [英] How to extract specific fields and values from a JSON with python?

查看:198
本文介绍了如何使用python从JSON提取特定字段和值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在迭代JSON,我想从该对象中提取以下字段:

I am iterating a JSON and I want to extract the following fields from this object:

  1. 编号,
  2. 开放日期
  3. 用户
  4. 门票状态
  5. 结束日期

我拥有的数据结构如下:

The data structure that I have is like the following :

filtered_data = 
[{'id': 1021972, 'Aging_Deferred_Transferred': '', 'Aging_Open_Issue': '0.94', 
'Aging_Un_investigated_Issue': '0.94', 'User': 'John P.', 'ModifiedOn': '2017-09-04 21:29:59', 
'Open_date':'2017-08-04 01:34:18','End_date':'2017-09-05 00:29:01',
'Ticket_status':'Transferred'},{'id': 1036722, 'Aging_Deferred_Transferred': '', 'Aging_Open_Issue': '0.12', 
'Aging_Un_investigated_Issue': '0.01', 'User': 'John P.', 'ModifiedOn': '2017-09-04 21:29:59', 
'Open_date':'2017-09-01 00:34:18','End_date':'',
'Ticket_status':'Researching'},{'id': 1015621, 'Aging_Deferred_Transferred': '', 'Aging_Open_Issue': '0.99', 
'Aging_Un_investigated_Issue': '0.11', 'User': 'John D.', 'ModifiedOn': '2017-06-05 12:19:59', 
'Open_date':'2017-01-01 00:00:18','End_date':'2017-09-01 20:20:57',
'Ticket_status':'Closed'}]

我想返回一个数据结构,例如Dictionary(如果您认为有一个更好的数据结构可以实现我的目标,请提及它),如下所示:

I want to return a data structure such as a Dictionary (if you think there is a better a data structure for achieving my goal please mention it) as the following:

dict_user_john_p = {'ID':1021972,'Open_date':'2017-08-04 01:34:18','Ticket_status':'Transferred','End_date':'2017-09-05 00:29:01',
'ID':1036722,'Open_date':'2017-09-01 00:34:18','Ticket_status':'Researching','End_date':''}

dict_user_john_D = {'ID':1015621,...,'End_date':'1015621'}

如何提取提到的特定字段并将其作为另一个数据结构(字典)返回?

How can I extract the mentioned specific fields and return them as another data structure (dictionary)?

推荐答案

filtered_data是一个普通列表,因此您可以使用普通索引或迭代方法从中访问各个字典.您可以将每个元素放入新的字典中,并按用户名键入:

filtered_data is an ordinary list, so you can access individual dictionaries from it using ordinary indexing or iteration. You can take each element and put them into a new dictionary, keyed by user name:

filtered_data = [{'id': 1021972, 'Aging_Deferred_Transferred': '', 'Aging_Open_Issue': '0.94', 
'Aging_Un_investigated_Issue': '0.94', 'User': 'John P.', 'ModifiedOn': '2017-09-04 21:29:59', 
'Open_date':'2017-08-04 01:34:18','End_date':'2017-09-05 00:29:01',
'Ticket_status':'Transferred'},{'id': 1036722, 'Aging_Deferred_Transferred': '', 'Aging_Open_Issue': '0.12', 
'Aging_Un_investigated_Issue': '0.01', 'User': 'John P.', 'ModifiedOn': '2017-09-04 21:29:59', 
'Open_date':'2017-09-01 00:34:18','End_date':'',
'Ticket_status':'Researching'},{'id': 1015621, 'Aging_Deferred_Transferred': '', 'Aging_Open_Issue': '0.99', 
'Aging_Un_investigated_Issue': '0.11', 'User': 'John D.', 'ModifiedOn': '2017-06-05 12:19:59', 
'Open_date':'2017-01-01 00:00:18','End_date':'2017-09-01 20:20:57',
'Ticket_status':'Closed'}]

data_by_user = {}
for d in filtered_data:
    data_by_user[d["User"]] = d

print(data_by_user["John P."])

现在,您可以通过用其名称索引新词典来访问John P的数据(或其他任何人的数据).

Now you can access John P's data (or anyone else's data) by indexing the new dictionary with their name.

通过构造明确仅从指定键中选择的新字典,您可以选择在新字典中将包含哪些键/值对:

you can be selective about which key/value pairs will be in the new dictionary, by constructing new dicts that explicitly only select from the keys you specify:

data_by_user = {}
for d in filtered_data:
    data_by_user[d["User"]] = {k:d[k] for k in ("id", "Open_date", "User", "Ticket_status", "End_date")}

这篇关于如何使用python从JSON提取特定字段和值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆