[英]Python json How to add data
我英语说得不好所以这个问题可能有点奇怪
{
"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
需要用双引号写成"arg3"
(单引号在 json 中无效),除非您之前定义了它的值。
json_data = {"arg3": {"1": "1", "2": "2"}}
进行更改,您应该能够加载并正确显示您的 JSON :
with open(f'output.json') as f:
a = json.load(f)
print(json.dumps(a, indent="\t"))
如果你执行json.dumps()
你会得到一个格式正确的 json 然后你可以调用print
来显示。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.