python - 迭代替换 json 文件中的值

标签 python

我必须执行一些测试来调整 json 文件的一些数字参数。 为了简单起见,我将所有这些值替换为字符串“variable”,然后执行以下操作:

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
                                        }
                                    ]
                                ]
                            }
                        ]
                    }
                }
            ]
        }
    ]
}

}

最佳答案

对分层/结构化数据进行字符串操作通常是一个坏主意,因为在很多情况下可能会破坏结构。由于您已经在解析 JSON,因此您可以扩展解码器以在解析期间专门处理您的情况,例如:

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())

这可以确保您正确解析 JSON,并且它不会替换(例如)恰好被称为“变量”的键。

请注意,如果 JSON 中的 "variable" 值多于 numeric_vals ,则会引发 StopIteration 异常- 如果您预计会遇到此类情况,您可以在 replace_values 中解开字典理解并处理此类情况。

关于python - 迭代替换 json 文件中的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54419232/

相关文章:

python - 阻止 rh-python35 替换 `python` 二进制文件中的 Python 2.7

python - 使用 multiprocessing.manager 的问题

python - 使用 python 将 QTreeWidgetItem 发送到函数

python - 如何将 max_length 添加到 allauth 用户名

python - 为什么 session cookie 在从域提供服务时有效,但在使用 IP 时却无效?

python - 根据 pandas 中的其他数据帧过滤一个数据帧

python - 如何从工具栏按钮单击和/或 ctrl + 鼠标滚轮缩放/缩放 QTextEdit 区域

将 HTML 表格转换为可读纯文本的 Python 解决方案

python - Windows 上的 Unicode 更改控制台字体

python - 在 Django Rest Framework 中列出具有动态字段的序列化程序