Python json如何添加数据
我英语说得不好 所以这个问题可能有点奇怪Python json如何添加数据,python,json,Python,Json,我英语说得不好 所以这个问题可能有点奇怪 { "arg1": { "1": "1", "2": "2" }, "arg2": { "1": "1", "2": "2" }, "arg3&
{
"arg1": {
"1": "1",
"2": "2"
},
"arg2": {
"1": "1",
"2": "2"
},
"arg3": {
"1": "1",
"2": "2"
}
}
我想以这种方式存储数据。我该怎么办
json_data = {arg3: {"1": "1", "2": "2"}}
with open(f'./Json/test.json', 'w', encoding='utf-8') as make_file:
json.dump(json_data, make_file, ensure_ascii=False ,indent="\t")
是这样吗?
如果你能告诉我,我将不胜感激
我不知道如何删除原始内容。您的代码运行良好。运行它时我看到的唯一问题是,
arg3
需要用双引号(单引号在json中无效)编写为“arg3”
,除非您之前定义了它的值
json_data={“arg3”:{“1”:“1”,“2”:“2”}
进行更改后,您应该能够通过以下方式加载并正确显示JSON:
以open(f'output.json')作为f:
a=json.load(f)
打印(json.dumps(a,indent=“\t”))
如果执行
json.dumps()
操作,您将获得格式正确的json,然后可以调用print
来显示它。欢迎使用<代码>缩进=4应该可以工作。