代码之家  ›  专栏  ›  技术社区  ›  Matina G

迭代替换JSON文件中的值

  •  1
  • Matina G  · 技术社区  · 6 年前

    我必须执行一些测试来调优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
                                            }
                                        ]
                                    ]
                                }
                            ]
                        }
                    }
                ]
            }
        ]
    }
    

    }

    2 回复  |  直到 6 年前
        1
  •  2
  •   zwer    6 年前

    通常,对分层/结构化数据执行字符串操作是一个坏主意,因为在许多情况下,您可能会破坏结构。由于您已经在解析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,并且它不会替换(例如)一个名为“variable”的键。

    当心,透,它会提高 StopIteration 如果有更多例外 "variable" JSON中的值比 numeric_vals -你可以在 replace_values 如果你希望遇到这样的情况,就要处理这种情况。

        2
  •  1
  •   Óscar López    6 年前

    你可以打电话给 json.loads() 在循环之外,您只需要这样做 一旦 最后:

    numeric_vals = [10, 20, 30, 40]
    
    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)
    

    也喜欢使用 string.replace 结束 re.sub ,按照这个 post .