在python中将json字符串转换为单个字典

2024-09-25 08:41:56 发布

您现在位置:Python中文网/ 问答频道 /正文

将多个JSON字符串转换为字典

正在尝试转换为以下格式

{"_id":[0,1....9],"name":["aimee Zank","Aurelia Menendez"...."Sanda Ryba"],"scores":[] so on}
{"_id":0,"name":"aimee Zank","scores":[{"score":1.463179736705023,"type":"exam"},{"score":11.78273309957772,"type":"quiz"},{"score":35.8740349954354,"type":"homework"}]}
{"_id":1,"name":"Aurelia Menendez","scores":[{"score":60.06045071030959,"type":"exam"},{"score":52.79790691903873,"type":"quiz"},{"score":71.76133439165544,"type":"homework"}]}
{"_id":2,"name":"Corliss Zuk","scores":[{"score":67.03077096065002,"type":"exam"},{"score":6.301851677835235,"type":"quiz"},{"score":66.28344683278382,"type":"homework"}]}
{"_id":3,"name":"Bao Ziglar","scores":[{"score":71.64343899778332,"type":"exam"},{"score":24.80221293650313,"type":"quiz"},{"score":42.26147058804812,"type":"homework"}]}
{"_id":4,"name":"Zachary Langlais","scores":[{"score":78.68385091304332,"type":"exam"},{"score":90.2963101368042,"type":"quiz"},{"score":34.41620148042529,"type":"homework"}]}
{"_id":5,"name":"Wilburn Spiess","scores":[{"score":44.87186330181261,"type":"exam"},{"score":25.72395114668016,"type":"quiz"},{"score":63.42288310628662,"type":"homework"}]}
{"_id":6,"name":"Jenette Flanders","scores":[{"score":37.32285459166097,"type":"exam"},{"score":28.32634976913737,"type":"quiz"},{"score":81.57115318686338,"type":"homework"}]}
{"_id":7,"name":"Salena Olmos","scores":[{"score":90.37826509157176,"type":"exam"},{"score":42.48780666956811,"type":"quiz"},{"score":96.52986171633331,"type":"homework"}]}
{"_id":8,"name":"Daphne Zheng","scores":[{"score":22.13583712862635,"type":"exam"},{"score":14.63969941335069,"type":"quiz"},{"score":75.94123677556644,"type":"homework"}]}
{"_id":9,"name":"Sanda Ryba","scores":[{"score":97.00509953654694,"type":"exam"},{"score":97.80449632538915,"type":"quiz"},{"score":25.27368532432955,"type":"homework"}]}

转换为具有多个值的单键。请在这里添加工作代码,而不是问题链接

How to add multiple values to a dictionary key in python?


Tags: tonameidtypequizscorehomeworkexam
1条回答
网友
1楼 · 发布于 2024-09-25 08:41:56

试试这个

import json

d = {}
with open('file1.txt') as f:
    for line in f:
        data = json.loads(line)
        for k, v in data.items():
            d.setdefault(k,[]).append(v)

或者可以使用defaultdict定义值为list的空dict

import json
from collections import defaultdict

d = defaultdict(list)
with open('file1.txt') as f:
    for line in f:
        data = json.loads(line)
        for k, v in data.items():
            d[k].append(v) 

输出:

print(d)

{'_id': [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], 'name': ['aimee Zank', 'Aurelia Menendez', 'Corliss Zuk', 'Bao Ziglar', 'Zachary Langlais', 'Wilburn Spiess', 'Jenette Flanders', 'Salena Olmos', 'Daphne Zheng', 'Sanda Ryba'], 'scores': [[{'score': 1.463179736705023, 'type': 'exam'}, {'score': 11.78273309957772, 'type': 'quiz'}, {'score': 35.8740349954354, 'type': 'homework'}], [{'score': 60.06045071030959, 'type': 'exam'}, {'score': 52.79790691903873, 'type': 'quiz'}, {'score': 71.76133439165544, 'type': 'homework'}], [{'score': 67.03077096065002, 'type': 'exam'}, {'score': 6.301851677835235, 'type': 'quiz'}, {'score': 66.28344683278382, 'type': 'homework'}], [{'score': 71.64343899778332, 'type': 'exam'}, {'score': 24.80221293650313, 'type': 'quiz'}, {'score': 42.26147058804812, 'type': 'homework'}], [{'score': 78.68385091304332, 'type': 'exam'}, {'score': 90.2963101368042, 'type': 'quiz'}, {'score': 34.41620148042529, 'type': 'homework'}], [{'score': 44.87186330181261, 'type': 'exam'}, {'score': 25.72395114668016, 'type': 'quiz'}, {'score': 63.42288310628662, 'type': 'homework'}], [{'score': 37.32285459166097, 'type': 'exam'}, {'score': 28.32634976913737, 'type': 'quiz'}, {'score': 81.57115318686338, 'type': 'homework'}], [{'score': 90.37826509157176, 'type': 'exam'}, {'score': 42.48780666956811, 'type': 'quiz'}, {'score': 96.52986171633331, 'type': 'homework'}], [{'score': 22.13583712862635, 'type': 'exam'}, {'score': 14.63969941335069, 'type': 'quiz'}, {'score': 75.94123677556644, 'type': 'homework'}], [{'score': 97.00509953654694, 'type': 'exam'}, {'score': 97.80449632538915, 'type': 'quiz'}, {'score': 25.27368532432955, 'type': 'homework'}]]}

相关问题 更多 >