我想从python中的json值创建字典列表。 json数据是
.button {
background : gray;
}
[data-url*=".nz"] .button{
background : green
}
我想将此数据放入包含dicts数组(json的“ data”部分)的dicts数组(name1,name2,name3)中,类似
{
"data": [
{
"date": "2017-07-28_15-54-10",
"name": "name1",
"state": "true"
},
{
"date": "2017-07-29_15-54-10",
"name": "name2",
"state": "true"
}
]
}
我得到了所有数据,我的代码是
[
{
"name1": [
{
"date": "2017-07-28_15-54-10"
},
{
"state": "true"
}
]
},
{
"name2": [
{
"date": "2017-07-28_15-54-10"
},
{
"state": "true"
}
]
}
]
我能够得到这个输出
for jn in data:
name = jn.get("name")
my_dict = {}
my_dict1 = {}
my_list = []
my_dict["date"] = jn.get("date")
my_dict1["state"] = jn.get("state")
my_list.append(my_dict) # dict for date
my_list.append(my_dict1) # dict for state
my_name_dict = {}
my_name_dict[name] = my_list # add the small dicts to the "name" dict
my_final_array.append(my_name_dict)
print(my_final_array)
我正在努力将这些字典添加到列表/数组中。当我打印 {
"name1": [
{
"date": "2017-07-28_15-54-10"
},
{
"state": "true"
}
]
},
{
"name2": [
{
"date": "2017-07-28_15-54-10"
},
{
"state": "true"
}
]
}
的值时,我只是得到字典的最后一行,而不是列表中的所有行。
我的代码也有些混乱,还有其他方法可以做到吗?
答案 0 :(得分:4)
使用简单的列表理解:
# if you are reading data from a file...
import json
path = "/path/to/json"
d = json.load(open(path, "r"))
# ...else
d = {
"data": [
{
"date": "2017-07-28_15-54-10",
"name": "name1",
"state": "true",
},
{
"date": "2017-07-29_15-54-10",
"name": "name2",
"state": "true",
}
]
}
records = [{x["name"]: [{"date": x["date"]}, {"state": x["state"]}]} for x in d["data"]]
records
>>> [{'name1': [{'date': '2017-07-28_15-54-10'}, {'state': 'true'}]},
{'name2': [{'date': '2017-07-29_15-54-10'}, {'state': 'true'}]}]
如果您打算向记录中添加更多键,则可以通过获取除name
以外的所有键来使此列表更通用,例如:
records = [
{x["name"]: [{k: v} for k, v in x.items() if k != "name"]}
for x in d["data"]
]
答案 1 :(得分:2)
您可以尝试以下方法吗?
json_data = {
"data": [
{
"date": "2017-07-28_15-54-10",
"name": "name1",
"state": "true",
},
{
"date": "2017-07-29_15-54-10",
"name": "name2",
"state": "true",
}
]
}
main_list = []
for val in json_data['data']:
temp_dict = {}
temp_key = val.pop('name')
temp_dict[temp_key] = [val]
main_list.append(temp_dict)
print(main_list)
输出:
[
{
'name1':[
{
'date':'2017-07-28_15-54-10',
'state':'true'
}
]
},
{
'name2':[
{
'date':'2017-07-29_15-54-10',
'state':'true'
}
]
}
]
答案 2 :(得分:1)
Pythonic的方法似乎是字典理解
a = {
"data": [
{
"date": "2017-07-28_15-54-10",
"name": "name1",
"state": "true"
},
{
"date": "2017-07-29_15-54-10",
"name": "name2",
"state": "true"
}
]
}
output = [{item['name']: {k: v for (k, v) in item.items() if k is not 'name'} for item in a['data']}]
output
> [{'name1': {'date': '2017-07-28_15-54-10', 'state': 'true'}, 'name2': {'date': '2017-07-29_15-54-10', 'state': 'true'}}]
答案 3 :(得分:1)
r = {
"data": [
{
"date": "2017-07-28_15-54-10",
"name": "name1",
"state": "true",
},
{
"date": "2017-07-29_15-54-10",
"name": "name2",
"state": "true",
}
]
}
data = r['data']
result = []
mkdic = lambda x: [{k: v} for k, v in x.items() if k != "name"]
for i in data:
result.append( {i.get("name"): mkdic(i) })
import pprint
pprint.pprint(result, indent = 2)
答案 4 :(得分:0)
您需要在for语句下创建最终列表,如下所示:
my_final_array = []
for jn in data:
name = jn.get("name")
my_dict = {}
my_dict1 = {}
my_list = []
my_dict["date"] = jn.get("date")
my_dict1["state"] = jn.get("state")
my_list.append(my_dict) # dict for date
my_list.append(my_dict1) # dict for state
my_name_dict = {}
my_name_dict[name] = my_list # add the small dicts to the "name" dict
my_final_array.append(my_name_dict)
print(my_final_array)