Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/355.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python json如何添加数据_Python_Json - Fatal编程技术网

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应该可以工作。