【发布时间】:2021-11-07 08:05:24
【问题描述】:
我有以下代码:
import json
with open("prefix.json", 'r+') as f:
data = json.load(f)
def changeprefix(id, prefix):
global data
if not id in data:
json.dump(data, open("prefix.json", "w"), indent = 4)
data[id] = prefix
else:
// Code to edit existing item
changeprefix("6", "?")
它会打开一个名为 prefix.json 的文件,我希望能够编辑现有项目,例如:
(Example.json)
{
"item": "234"
}
如何将item 更改为py 中的其他内容,例如更改为text here 然后保存文件?
提前致谢! (另外,如果你不喜欢我的问题,至少告诉我我可以改进什么,我 13 岁,当人们不喜欢我的帖子而没有上下文时我不知道该怎么办)
【问题讨论】:
-
data["item"] = "text here"?
标签: python json python-3.x