3 回答
TA贡献1836条经验 获得超13个赞
迭代字典的值:
with open(json_file_name) as input: jsonData = json.load(input) data = [(values['city'], values['label']) for values in jsonData.values()] print(data
)
出去:
[('Berlin.', 'Capital'), ('Frankfurt.', 'Economic Capital'), ('Paris.', 'Capital')]
TA贡献1784条经验 获得超8个赞
尝试
data = {
"5c7a3dd5-29e4-4f5b-9dc8-d2d451906cf1": {
"id": "1d14eba1-de9e-4e53-9e9e-619432fd815b",
"opinion_id": "3063e6ff-1483-4daf-8e80-41afa43b6348",
"paragraph_id": "e6d419fe-5017-47f6-acf1-0e74034aed4a",
"position": 7,
"city": "Berlin.",
"label": "Capital"
},
"0c0adcad-992f-4c30-aa58-7bf539f8d649": {
"case_id": "6f6e70f7-3257-4f26-a8fa-8cf0e5f97920",
"opinion_id": "71b435ff-112c-470b-a36b-c552e54a5605",
"paragraph_id": "e6d419fe-5017-47f6-acf1-0e74034aed4a",
"position": 3,
"city": "Frankfurt.",
"label": "Economic Capital"
},
"de968cdf-d865-4049-9f7d-f9a68be27432": {
"case_id": "2d749a58-412c-4bc8-b268-7d5f7bec3d9a",
"opinion_id": "1836c972-d6f7-420b-ba33-8f863cfca482",
"paragraph_id": "e6d419fe-5017-47f6-acf1-0e74034aed4a",
"position": 4,
"city": "Paris.",
"label": "Capital"
}
}
city_n_label = [(x['city'],x['label']) for x in data.values()]
print(city_n_label)
输出
[('Berlin.', 'Capital'), ('Frankfurt.', 'Economic Capital'), ('Paris.', 'Capital')]
TA贡献1810条经验 获得超4个赞
尝试这个
with open(json_file_name) as input:
jsonData = json.load(input)
for k, v in jsonData.items():
print(k) # prints dynamic key
print(v) # prints its corresponding obj
print(v['city']) # will print city of the particular key
print(v['label']) # will print label of the particular key
添加回答
举报