python - 使用python修改json文件
问题描述
嗨,我有一个像这样的 json 文件。
{
a : {
b:"b_value",
c:"c_value"
}
}
我想读取json文件并使用python进行修改,所以文件内容将是这样的
{
a : {
x:{
b:"b_value",
c:"c_value"
}
}
}
请大家帮忙谢谢。
解决方案
- 将json读入字典:
import json
with open("json_file.json", "r") as f:
data = json.load(f)
- 使用创建您想要的结构
data
:
manipulated_data = {"a" : { "x": data["a"] } }
- 将您的文件写入
manipulated_data
文件:
with open("json_file.json", "w") as f:
json.dump(f, manipulated_data)
推荐阅读
- node.js - listObjectsV2 使用 nodejs 列出嵌套“文件夹”中的所有对象
- github - 你能从终端触发一个 github webhook
- r - 如何用包latex2exp在xlab中做概率符号?
- bash - 自动重新启动已终止的作业 [bash]
- python - 未捕获的类型错误:无法读取未定义的属性(读取“开始”)
- bayesian - 如何在贝叶斯先验上将下限指定为 0
- reactjs - 在 Javascrip React 中创建 BcG 矩阵
- bash - 如何在bash中提取字符串?
- vue.js - 模型名称的 Prop 无效,字符串输入显示为“未定义”
- python - 在最小值的索引处返回二进制“1”