我想在JSON文件中的列表中添加一个字典
reading = []
reading["game_review"] = {
"name" : your_name,
"time" : round(time_result, 2),
"level" : level+1
}
with open("stat.json", "a") as stats:
json.dump(reading, stats)
每次我运行代码时,会在JSON文件中创建另一个dictionary,并将自己放在我已经拥有的dictionary旁边,我希望它将自己添加到dictionary中的一个列表中
编辑时间:
with open("stat.json", "r") as stat_read:
reading = json.loads(stat_read.read())
reading["game_review"] = {
"name" : your_name,
"time" : round(time_result, 2),
"level" : level+1
}
with open("stat.json", "a") as stats:
json.dump(reading, stats)
如果您想这样做,请读取该文件,解析JSON内容,然后将JSON追加到列表中。然后重新写入文件。 假设一个JSON文件有以下内容
然后你可以做以下事情
文件中的最终内容将是
相关问题 更多 >
编程相关推荐