使用其他两个字典更新嵌套字典

时间:2019-03-05 03:26:14

标签: python dictionary

我有一个嵌套的字典,我希望使用两个字典进行更新。我要填充的空字典如下:-

master_config_skeleton = {
"ingestion_config":
    {
    "location":{}, 
    "start_sequence":{}, 
    "datafeed":
        {
        "t04047":
            {
            "validation":
                {
                "triple_check":{},
                "record_count_validation":{}
                }, 
            "date_pattern":{}, 
            "cdc_config": {}
            }
        }
    }
}

保存遍历路径的字典之一如下。

    traversal_path_dict = {
        'cdc_config': "['ingestion_config']['datafeed']['t04047']['cdc_config']",
        'datafeed': "['ingestion_config']['datafeed']",
        'date_pattern': "['ingestion_config']['datafeed']['t04047']['date_pattern']",
        'ingestion_config': "['ingestion_config']",
        'location': "['ingestion_config']['location']",
        'record_count_validation': "['ingestion_config']['datafeed']['t04047']['validation']['record_count_validation']",
        'start_sequence': "['ingestion_config']['start_sequence']",
        't04047': "['ingestion_config']['datafeed']['t04047']",
        'triple_check': "['ingestion_config']['datafeed']['t04047']['validation']['triple_check']",
        'validation': "['ingestion_config']['datafeed']['t04047']['validation']"}

另一个实际保存数据的字典如下:-

master_data_dict = {   'cdc_config': {   'active_table': 'db_b2b_curated_flexcab_active.t04047',
                      'custom_cdc_query': '',
                      'delete_filters': '["","",""]',
                      'delete_query': '',
                      'history_table': 'db_b2b_curated_flexcab_historical.t04047_historical',
                      'key_cols': '[INVC_ARGT_ID, BILL_CYCLE_EFF_DT, '
                                  'BILL_CYCLE_CD]',
                      'start_date_col': '[BILL_CYCLE_EFF_DT]',
                      'update_ts_col': 'bill_cycle_end_dt'},
    'date_pattern': {'ord_end_dt': '%d.%m.%Y', 'ord_sta_dt': '%d.%m.%Y'},
    'ingestion_config': {   'app': 'Replicator',
                            'feed_id': 2382,
                            'hdfs_home_dir': '/data/b2b',
                            'seq_type': 'SEQUENCE',
                            'source': 'FLEXCAB'},
    'location': {   'base_dir': '/home/sfxuser/B2B_DATALAKE/data/b2b/land/FLEXCAB',
                    'error_dir': 'Error',
                    'input_dir': 'Input',
                    'unzipped_dir': 'src'},
    'record_count_validation': {   'footer': 'N',
                                   'header': 'N',
                                   'rec_cnt_pos_ctl': '0:10'},
    'start_sequence': {'initial_seq_id': '00000001', 'length': 8},
    't04047': {   'cdc': 'N',
                  'config_path': '/apps/b2b_datalake/legacy/FLEXCAB/cfg/CSV/t04047.csv',
                  'format': 'fixedwidth'},
    'triple_check': {   'file_count': 3,
                        'file_extensions': '[zip, ctl, eot]',
                        'file_pattern': 'FLEXCAB_B2B_T04047_O_(<batch_seq>)_*'},
    'validation': {'file_date_format': '%Y%m%d%H%M%S'}}

您会注意到,并非我要填充的字典中的所有键都具有值。第二个字典基本上是一个从Excel生成的熊猫数据帧,其中包含键值对。 excel工作簿的所有工作表都存储在单独的列表中:-

sheets = ['ingestion_config', 'location', 'start_sequence', 'datafeed', 't04047', 'validation', 'triple_check', 'record_count_validation', 'date_pattern', 'cdc_config']

到目前为止我已经尝试过:-

for sheet in sheets:
    master_config_skeleton[traversal_path_dict[sheet]].update(master_data_dict[sheet])

但是由于方括号限定了值,所以出现了关键错误。

1 个答案:

答案 0 :(得分:0)

您是否尝试过使用.get方法?如果没有here是指向python docs的链接,则在未找到键的情况下返回None,这将防止键错误。