python修改json
时间: 2023-08-03 18:03:53
浏览: 84
在Python中,可以使用json模块来修改JSON文件。下面是一个示例[代码]([http]([http](https://geek.csdn.net/educolumn/68f28ca3131d47699e5902c5d3d74d51?spm=1055.2569.3001.10083)s://geek.csdn.net/educolumn/68f28ca3131d47699e5902c5d3d74d51?spm=1055.2569.3001.10083)s://geek.csdn.net/educolumn/1572ef9b473b4e00f6b2bf6d428b7c27?spm=1055.2569.3001.10083):
```python
import json
# 读取json文件
with open("my_dict.json", "r") as f:
data = f.read()
result = json.loads(data)
# 修改json文件里面的[属性](https://geek.csdn.net/educolumn/5280ca4eda584c6baf7c2ec8010927f9?spm=1055.2569.3001.10083)
result\['name'\] = 'Lois'
result\['age'\] = 20
# 将修改后的[数据](https://geek.csdn.net/educolumn/88edee002202697cde4e0b7ae3d009aa?spm=1055.2569.3001.10083)保存到json文件
with open("my_dict.json", "w") as fp:
json.dump(result, fp)
```