[英]Convert the nested json into a dictionary format with no nested objects
I have the input data in below format: 我有以下格式的输入数据:
data = [[u'Richard', u'48', [u'Josh', u'Beth'], {u'city': u'Seattle', u'Disability': u'no', u'enterprenuer': u'yes'}], [u'Bryan', u'32',[], {u'city': u'NY', u'enterprenuer': u'no', u'wfh': u'yes', u'disability': u'no', u'Visa': u'no'}]]
Which later on doing json.dumps
becomes: 稍后在执行json.dumps
变为:
[["Richard", "48", ["Josh", "Beth"], {"city": "Seattle", "enterprenuer": "yes", "Disability": "no"}], ["Bryan", "32", [], {"Visa": "no", "city": "NY", "wfh": "yes", "enterprenuer": "no", "disability": "no"}]]
Also, I have another list which holds the keys for the dict: 另外,我还有另一个列表,该列表包含该字典的键:
key_list = ["Name", "Age", "Children", "details"]
I tried the below code: 我尝试了以下代码:
list_of_dicts = []
for d in data:
dict = {}
for i in range(0, len(key_list)-1):
dict[key_list[i]] = d[i]
list_of_dicts.append(dict)
With this I was able to get new_dict
: 这样我就可以得到new_dict
:
[{'Age': u'48', 'Name': u'Richard', 'Children': [u'Josh', u'Beth']}, {'Age': u'32', 'Name': u'Bryan', 'Children': []}]
But I am not able to get the nested dict from data
into the new_dict
without the need to run code on it again. 但是我无法将嵌套的dict从data
到new_dict
而无需再次在其上运行代码。 I don't want to run operation multiple times. 我不想多次运行操作。 Also, I was thinking if there's any better way to remove the nested list as well but after multiple hit and trial I got side tracked and messed up my code. 另外,我也在考虑是否还有更好的方法来删除嵌套列表,但是在多次尝试后,我被跟踪并弄乱了我的代码。
This is the expected output: 这是预期的输出:
[{"Name":"Richard","Age":"48","Children":"Josh,Beth","city":"Seattle","enterprenuer":"yes","Disability":"no"},{"Name":"Bryan","Age":"32","Children":"","Visa":"no","city":"NY","wfh":"yes","enterprenuer":"no","disability":"no"}]
you can try: 你可以试试:
1) in python3: 1)在python3中:
from pprint import pprint
data = [["Richard", "48", ["Josh", "Beth"], {"city": "Seattle", "enterprenuer": "yes", "Disability": "no"}], ["Bryan", "32", [], {"Visa": "no", "city": "NY", "wfh": "yes", "enterprenuer": "no", "disability": "no"}]]
key_list = ["Name", "Age", "Children", "details"]
pprint([dict(zip(key_list[:2], e[:2]), **{key_list[2]: ','.join(e[2])}, **e[3]) for e in data])
output: 输出:
[{'Name': 'Richard',
'Age': '48',
'Children': 'Josh,Beth',
'city': 'Seattle',
'Disability': 'no',
'enterprenuer': 'yes'},
{'Name': 'Bryan',
'Age': '32',
'Children': '',
'city': 'NY',
'enterprenuer': 'no',
'wfh': 'yes',
'disability': 'no',
'Visa': 'no'}]
2) in python2: 2)在python2中:
pprint([dict(zip(key_list[:2], e[:2]), **dict([(key_list[2], ','.join(e[2]))], **e[3])) for e in data])
output: 输出:
[{'Age': '48',
'Children': 'Josh,Beth',
'Disability': 'no',
'Name': 'Richard',
'city': 'Seattle',
'enterprenuer': 'yes'},
{'Age': '32',
'Children': '',
'Name': 'Bryan',
'Visa': 'no',
'city': 'NY',
'disability': 'no',
'enterprenuer': 'no',
'wfh': 'yes'}]
data = [["Richard", "48", ["Josh", "Beth"], {"city": "Seattle", "enterprenuer": "yes", "Disability": "no"}], ["Bryan", "32", [], {"Visa": "no", "city": "NY", "wfh": "yes", "enterprenuer": "no", "disability": "no"}]]
key_list = ["Name", "Age", "Children", "details"]
out = []
for item in data:
d = {}
out.append(d)
for value, keyname in zip(item, key_list):
if isinstance(value, dict):
d.update(**value)
elif isinstance(value, list):
d[keyname] = ','.join(value)
else:
d[keyname] = value
from pprint import pprint
pprint(out)
Prints: 印刷品:
[{'Age': '48',
'Children': 'Josh,Beth',
'Disability': 'no',
'Name': 'Richard',
'city': 'Seattle',
'enterprenuer': 'yes'},
{'Age': '32',
'Children': '',
'Name': 'Bryan',
'Visa': 'no',
'city': 'NY',
'disability': 'no',
'enterprenuer': 'no',
'wfh': 'yes'}]
You can use simple unpacking: 您可以使用简单的拆包:
data = [[u'Richard', u'48', [u'Josh', u'Beth'], {u'city': u'Seattle', u'Disability': u'no', u'enterprenuer': u'yes'}], [u'Bryan', u'32',[], {u'city': u'NY', u'enterprenuer': u'no', u'wfh': u'yes', u'disability': u'no', u'Visa': u'no'}]]
key_list = ["Name", "Age", "Children", "details"]
r = [{**dict(zip(key_list[:-1], a[:-1]+[','.join(a[-1])])), **b} for *a, b in data]
Output: 输出:
[{'Name': 'Richard', 'Age': '48', 'Children': 'Josh,Beth', 'city': 'Seattle', 'Disability': 'no', 'enterprenuer': 'yes'}, {'Name': 'Bryan', 'Age': '32', 'Children': '', 'city': 'NY', 'enterprenuer': 'no', 'wfh': 'yes', 'disability': 'no', 'Visa': 'no'}]
Edit: Python2.7 solution: 编辑:Python2.7解决方案:
data = [[u'Richard', u'48', [u'Josh', u'Beth'], {u'city': u'Seattle', u'Disability': u'no', u'enterprenuer': u'yes'}], [u'Bryan', u'32',[], {u'city': u'NY', u'enterprenuer': u'no', u'wfh': u'yes', u'disability': u'no', u'Visa': u'no'}]]
key_list = ["Name", "Age", "Children", "details"]
r = [dict(zip(key_list[:-1], i[:2]+[','.join(i[2])])+i[-1].items()) for i in data]
Output: 输出:
[{u'city': u'Seattle', 'Name': u'Richard', 'Age': u'48', u'enterprenuer': u'yes', u'Disability': u'no', 'Children': u'Josh,Beth'}, {u'city': u'NY', u'wfh': u'yes', 'Name': u'Bryan', 'Age': u'32', u'enterprenuer': u'no', u'disability': u'no', u'Visa': u'no', 'Children': ''}]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.