python - 迭代替换json文件中的值
问题描述
我必须执行一些测试来调整 json 文件的一些数字参数。为简单起见,我将所有这些值替换为字符串“变量”,然后执行以下操作:
numeric_vals = [10,20, 30, 40] # numeric values to replace in that order
with open ('mypath') as my_file:
json_str = my_file.read()
for i in numeric_vals:
json_str = json_str.replace("\"variable\"", str(i), 1)
c = json.loads(json_str) #loading in order to work with
这工作正常,但有没有更有效的方法来做到这一点?需要替换的值的深度是可变的,并且可能在列表中等。我的 json 文件是 15Kb,我需要测试很多(真的很多!)配置。在每次测试中,大约需要替换 200 个变量。我在 python 2.7 上,但 python 3.5 也是一个选项。谢谢你的帮助!
编辑 :
这是我的字典样本。需要注意的是,真实的东西要更长更深:
{
"1": {
"transition": {
"value": "variable", # edit here
"unite": "sec"
},
"sequence": [
{
"step": "STEP",
"name": "abc",
"transition": {
"value": "variable", #edit here
"unite": "sec"
},
"entity": "aa",
"_equipement": 3,
"_value": 0
},
{
"step": "FORK",
"BRANCHES": [
{
"": {
"sequence": [
{
"step": "STEP",
"name": "klm",
"transition": {
"value": "variable", # edit here
"unite": "sec"
},
"entity": "vvv",
"_equipement": 10,
"_value": 0,
"conditions": [
[
{
"name": "ddd",
"type": "el",
"_equipement": 7,
"_value": 1
}
]
]
}
]
}
},
{
"SP": {
"sequence": [
{
"step": "STEP",
"name": "bbb",
"transition": {
"value": "variable", # edit here
"unite": "sec"
},
"entity": "bb",
"_equipement": 123,
"_value": 0,
"conditions": [
[
{
"name": "abcd",
"entity": "dgf",
"type": "lop",
"_equipement": 7,
"_valeur": 0
}
]
]
}
]
}
}
]
}
]
}
}
解决方案
It's generally a bad idea to do string operations on hierarchical/structured data as there may be many cases where you can break the structure. Since you're already parsing your JSON you can extend the decoder to specifically deal with your case during parsing, e.g.:
numeric_vals = [10, 20, 30, 40] # numeric values to replace in that order
SEARCH = 'variable'
REPLACE = iter(numeric_vals) # turn into an iterator for sequential access
def replace_values(value):
return {k: next(REPLACE) if v == SEARCH else v for k, v in value.items()}
with open('path/to/your.json') as f:
a = json.JSONDecoder(object_hook=replace_values).decode(f.read())
This ensures you're properly parsing your JSON and that it won't replace, for example, a key that happens to be called 'variable'.
Beware, tho, that it will raise an StopIteration
exception if there are more "variable"
values in the JSON than there are numeric_vals
- you can unravel the dict comprehension in replace_values
and deal with such case if you expect to encounter such occurrences.
推荐阅读
- html - 我正在尝试映射图像,以便它将某些部分链接到不同的页面我需要另一双眼睛
- jprofiler - Jprofiler 是否分析 gc 日志文件以显示伊甸园空间利用率与终身利用率等内容?
- c - 带逻辑运算符的有符号和无符号整数
- npm - 如何告诉 NPM 在哪里安装特定文件?
- java - 如果需要超过特定时间,Java 停止计划任务
- npm - 从 Bintray 存储库中解析 NPM 包
- ios - 如何检测用户何时拒绝启用位置服务超过 2 次?
- javascript - Node.js http 请求返回 body/msg 为空
- java - IOException:权限被拒绝(但我有权限)
- ruby-on-rails-5 - 有没有办法在链停止时中止销毁和更新记录?