在检测工作上,大家一般会触碰到预期成果数据信息与具体結果数据信息一致性较为的检测情景,针对数据信息繁杂巨大的较为工作中。假如依靠人力实行,成本费非常高,无法确保实行效果的一致性(数次实行很有可能会出现误差),可重复性极高。因而,大家常常必须考虑到怎样使用自动化技术专用工具完成数据信息较为。

文中介绍了怎样完成Json和词典算法设计的递归算法分析。

JSON的二种算法设计。

1.1的结合。键值对能够解释为Python语言表达中的词典,如下图所显示。

python解析json文件并提取-python处理excel教程实例-第1张图片2.井然有序结合能够解释为Python语言表达中的List,如下图所显示。

python解析json文件并提取-python处理excel教程实例-第2张图片编码实例

使我们以下边这一繁杂的Json为例子。

test_json = { "code": 200, "data": [ { "apps": { "app_op_seq": [ { "action": "点一下", "module_name": "淘宝聚划算", "module_type": "resource" } ] }, "content": { "des": { "company_name": "NIKE", "intent": [ "full" ] }, "rel": [ { "des": { "person_name": "皇甫玖林", "political_status": "冠军vip会员" }, "ont": [ "Company", "Person" ], "relIdx": [ 0, "8-9" ], "relName": "皇甫", "segs": [ "耐克篮球鞋" ] } ], "segs": [ "耐克篮球鞋" ] }, "content_op": "查看" } ]}

完成源码

def json_generator(indict, key_value=None): "" 递归算法分析 Json 数据信息 :param indict: :param key_value: :return: "" key_value = key_value[:] if key_value else [] if isinstance(indict, dict): for key, value in indict.items(): tier = - 1 if isinstance(value, dict) and value != {}: if len(value) == 0: yield key_value [key, '{}'] else: for d in json_generator(value, key_value [key]): yield d elif isinstance(value, list) and value != []: if len(value) == 0: yield key_value [key, '[]'] else: for v in value: tier = tier 1 for d in json_generator(v, key_value [key '[{0}]'.format(tier)]): yield d else: yield key_value [key, value] else: if not key_value: yield indict else: yield key_value [indict]def structure_flow_sub(json_gen_obj): "" 分析結果 恢复出厂设置輸出 :param json_gen_obj: :return: JsonPath:data[0].apps.app_op_seq[0].action "" structure = {} for i in json_generator(json_gen_obj): if '.'.join(i[:-1]) in structure.keys() and not isinstance(structure['.'.join(i[:-1])], list): structure['.'.join(i[:-1])] = [structure['.'.join(i[:-1])]] structure['.'.join(i[:-1])].append(i[-1]) elif '.'.join(i[:-1]) in structure.keys() and isinstance(structure['.'.join(i[:-1])], list): structure['.'.join(i[:-1])].append(i[-1]) else: structure['.'.join(i[:-1])] = i[-1] return structuredef json_path_parse(json_gen_obj): "" Json途径分析 :param json_gen_obj: :return: "" structure_List = [] if isinstance(json_gen_obj, dict): structure = structure_flow_sub(json_gen_obj) structure_List.append(structure) return structure_List if isinstance(json_gen_obj, list): for i in json_gen_obj: result = structure_flow_sub(i) structure_List.append(result) return structure_Listif ._name._ == '._main._': all_leaf_node_dict = json_path_parse(test_json) line_number = 0 for k, v in all_leaf_node_dict[0].items(): line_number = 1 print("{line_number} JsonPath:{json_path} Value:{value} ".format( line_number=line_number, json_path=k, value=v))

剖析結果如下所示。

1 JsonPath:code Value:200 2 JsonPath:data[0].apps.app_op_seq[0].action Value:点一下 3 JsonPath:data[0].apps.app_op_seq[0].module_name Value:淘宝聚划算 4 JsonPath:data[0].apps.app_op_seq[0].module_type Value:resource 5 JsonPath:data[0].content.des.company_name Value:NIKE 6 JsonPath:data[0].content.des.intent[0] Value:full 7 JsonPath:data[0].content.rel[0].des.person_name Value:皇甫玖林 8 JsonPath:data[0].content.rel[0].des.political_status Value:冠军vip会员 9 JsonPath:data[0].content.rel[0].ont[0] Value:Company 10 JsonPath:data[0].content.rel[0].ont[1] Value:Person 11 JsonPath:data[0].content.rel[0].relIdx[0] Value:0 12 JsonPath:data[0].content.rel[0].relIdx[1] Value:8-9 13 JsonPath:data[0].content.rel[0].relName Value:皇甫 14 JsonPath:data[0].content.rel[0].segs[0] Value:耐克篮球鞋 15 JsonPath:data[0].content.segs[0] Value:耐克篮球鞋 16 JsonPath:data[0].content_op Value:查看

评论(0条)

刀客源码 游客评论