如何基于Header结构从嵌套字典提取并整合DTC数据?
问题解决:按照指定Header结构从嵌套字典提取DTC数据
给定资源
嵌套字典数据
Nested_Dict = { 'candela_samples_generic': { 'drc_dtcs': { 'domain_name': 'TEMPLATE-DOMAIN', 'dtc_all': { '0x930001': { 'identification': { 'udsDtcValue': '0x9300', 'FaultType': '0x11', 'description': 'GNSS antenna short to ground' }, 'functional_conditions': { 'failure_name': 'short_to_ground', 'mnemonic': 'DTC_GNSS_Antenna_Short_to_ground' } }, '0x212021': { 'identification': { 'udsDtcValue': '0x2120', 'FaultType': '0x21', 'description': 'ECU internal Failure' }, 'functional_conditions': { 'failure_name': 'short_to_ground', 'mnemonic': 'DTC_GNSS_Antenna_Short_to_ground' } } } } } }
Header结构定义
Header = { 'dtc_all': { 'DiagnosticTroubleCodeUds': {'udsDtcValue': None, 'FaultType': None}, 'dtcProps': {'description': None}, 'DiagnosticTroubleCodeObd': {'failure_name': None} } }
子键列表
SubkeyList = ['0x930001','0x240001']
数据提取函数
def get_value_nested_dict(nested_dict, main_key, sub_key, element): results = [] def search_for_element(sub_dict, looking_for_element): if isinstance(sub_dict, dict): for k, v in sub_dict.items(): if k == element and looking_for_element: results.append(v) else: search_for_element(v, looking_for_element) def search_nested_dict(current_dict): if isinstance(current_dict, dict): for key, value in current_dict.items(): if key == main_key: if sub_key in value: search_for_element(value[sub_key], True) else: search_for_element(value, False) else: search_nested_dict(value) search_nested_dict(nested_dict) return results
需求说明
依次从SubkeyList中取出子键,从Header中取出主键(如dtc_all),遍历Header内的所有字段(如udsDtcValue、FaultType等),调用get_value_nested_dict函数提取对应值,构建与Header键顺序一致的新字典New_Dict。预期输出示例:
New_Dict = { 'dtc_all': { '0x930001': { 'DiagnosticTroubleCodeUds': {'udsDtcValue': '0x9300', 'FaultType': '0x11'}, 'dtcProps': {'description': 'GNSS antenna short to ground'}, 'DiagnosticTroubleCodeObd': {'failure_name': 'short_to_ground'} }, '0x240001': { 'DiagnosticTroubleCodeUds': {'udsDtcValue': None, 'FaultType': None}, 'dtcProps': {'description': None}, 'DiagnosticTroubleCodeObd': {'failure_name': None} } } }
实现方案
通过遍历Header的层级结构,结合给定的提取函数逐个获取对应值,最终组装成目标字典。代码实现如下:
# 初始化New_Dict New_Dict = {} # 获取Header中的主键(这里是dtc_all) for main_key in Header: New_Dict[main_key] = {} # 获取当前主键下的结构模板 dtc_template = Header[main_key] # 遍历SubkeyList中的每个子键 for sub_key in SubkeyList: # 为每个子键初始化结构 New_Dict[main_key][sub_key] = {} # 遍历模板中的每个一级节点(如DiagnosticTroubleCodeUds) for section, fields in dtc_template.items(): New_Dict[main_key][sub_key][section] = {} # 遍历节点下的每个字段(如udsDtcValue) for field in fields: # 调用提取函数获取值,取结果列表的第一个元素(如果有结果),否则为None value = get_value_nested_dict(Nested_Dict, main_key, sub_key, field) New_Dict[main_key][sub_key][section][field] = value[0] if value else None # 打印结果 import pprint pprint.pprint(New_Dict)
代码说明
- 初始化字典:先创建
New_Dict的顶层结构,匹配Header的主键。 - 遍历子键:对每个子键,按照Header的结构为其创建对应的层级。
- 字段提取:针对每个字段调用
get_value_nested_dict,若返回结果不为空则取第一个值(因为函数返回列表),否则保留None。 - 结构对齐:严格遵循Header的键顺序和层级,确保最终字典结构与需求一致。
内容的提问来源于stack exchange,提问作者shekhar sabale
相关产品推荐
相关产品推荐

