1.如何同时替换json多个指定key的value
import json from jsonpath_ng import parse def join_paths(regx_path,new_value,dict_replace): """ eg: join_paths(regx_path='$..host..namespace', new_value="9999999999", dict_replace=pydict) :param regx_path: the path of replaced key :param new_value: the new value of key to be replaced :param dict_replace: the initial_dict that to be replaced :return: dict """ data = dict_replace jsonpath_expr = parse(regx_path) str_path_list=[str(match.full_path) for match in jsonpath_expr.find(dict_replace)] def cast_dict_path(path_list): cast_list = [] for str_path in path_list: path_split_list=str_path.split('.') path = '' for i in path_split_list: if i.count('[')==1 and i.count(']')==1: path=path+'[%s]'%eval(i)[0] else: path=path+"['%s']"%i cast_list.append(path) #[ "['role_parameters']['guest']['args']['data']['train_data'][0]['namespace']" ] return cast_list cast_paths=cast_dict_path(str_path_list) for i in cast_paths: if isinstance(new_value,str): fullpath="data"+i+"='%s'"%new_value abs_path=fullpath exec(abs_path) if isinstance(new_value,(int,list,float)): fullpath = "data" + i + "={}".format(new_value) abs_path=fullpath exec(abs_path) return data def muti_replace(rep_list,initial_dict:dict): """ format of rep_list: [ (regx_path1 ,new_value1) ], (regx_path2 ,new_value2 ) ] for example: >> final_dict=muti_replace([('$..hetero_lr_0..eps',0.7777),('$..host..namespace',8888888)],initial_dict=pydict) initial_dict :the key need to replaced dict ,type dict """ dict_list=[] for i in rep_list: regx_path ,new_value=i[0],i[1] dict_next=join_paths(regx_path,new_value,dict_replace=initial_dict) dict_list.append(dict_next) for k in dict_list: initial_dict.update(k) print(json.dumps(initial_dict,indent=5)) return initial_dict if __name__ == '__main__': final_dict=muti_replace([('$..hetero_lr_0..eps',0.7777),('$..host..namespace',8888888)],initial_dict=pydict)
测试数据:

{ "initiator": { "role": "guest", "party_id":9997 }, "job_parameters": { "work_mode": 1 }, "role": { "guest": [ 9997 ], "host": [ 9997 ], "arbiter": [ 9997 ] }, "role_parameters": { "guest": { "args": { "data": { "train_data": [ { "name": "breast_guest", "namespace": "breast_guest" } ] } }, "dataio_0": { "with_label": [true], "label_name": ["y"], "label_type": ["int"], "output_format": ["dense"], "missing_fill": [true], "outlier_replace": [true] }, "feature_scale_0": { "method": ["min_max_scale"] }, "hetero_feature_binning_0": { "method": ["quantile"], "compress_thres": [10000], "head_size": [10000], "error": [0.001], "bin_num": [10], "cols": [-1], "adjustment_factor": [0.5], "local_only": [false], "transform_param": { "transform_cols": [-1], "transform_type": ["bin_num"] } }, "hetero_feature_selection_0": { "select_cols": [-1], "filter_methods": [[ "unique_value", "iv_value_thres", "coefficient_of_variation_value_thres", "iv_percentile", "outlier_cols" ]], "local_only": [false], "unique_param": { "eps": [1e-6] }, "iv_value_param": { "value_threshold": [1.0] }, "iv_percentile_param": { "percentile_threshold": [0.9] }, "variance_coe_param": { "value_threshold": [0.3] }, "outlier_param": { "percentile": [0.95], "upper_threshold": [10] } }, "evaluation_0": { "eval_type": ["binary"], "pos_label": [1] } }, "host": { "args": { "data": { "train_data": [ { "name": "breast_host", "namespace": "breast_host" } ] } }, "dataio_0": { "with_label": [false], "output_format": ["dense"], "outlier_replace": [true] }, "feature_scale_0": { "method": ["standard_scale"], "need_run": [false] }, "hetero_feature_binning_0": { "method": ["quantile"], "compress_thres": [10000], "head_size": [10000], "error": [0.001], "bin_num": [10], "cols": [-1], "adjustment_factor": [0.5], "local_only": [false], "transform_param": { "transform_cols": [-1], "transform_type": ["bin_num"] } }, "hetero_feature_selection_0": { "select_cols": [-1], "filter_methods": [[ "unique_value", "iv_value_thres", "coefficient_of_variation_value_thres", "iv_percentile", "outlier_cols" ]], "local_only": [false], "unique_param": { "eps": [1e-6] }, "iv_value_param": { "value_threshold": [1.0] }, "iv_percentile_param": { "percentile_threshold": [0.9] }, "variance_coe_param": { "value_threshold": [0.3] }, "outlier_param": { "percentile": [0.95], "upper_threshold": [10] } }, "evaluation_0": { "need_run": [true] } } }, "algorithm_parameters": { "feature_scale_0": { "need_run": true }, "hetero_feature_binning_0": { "need_run": true }, "hetero_feature_selection_0": { "need_run": true }, "hetero_lr_0": { "penalty": "L2", "optimizer": "rmsprop", "eps": 1e-5, "alpha": 0.01, "max_iter": 10, "converge_func": "diff", "batch_size": -1, "learning_rate": 0.15, "init_param": { "init_method": "random_uniform" }, "cv_param": { "n_splits": 5, "shuffle": false, "random_seed": 103, "need_cv": false } } } }