python by using a function of the depth of the dictionary search simplify (reduce the cycle times)

Wherein the determination involves a number of conditions:

Cycle to achieve: (dict, sheet dictionaries are mainly row is a row in the dictionary sheet bottom of the list of data, which is roughly a screening process data

            for Key in dict.keys ():
                 IF Key == " Date " : 
                    dict [key] .append (Sheet [Key]) 
                elif of the type (dict [key]) = of the type (dict):! # If dict [key] not a dictionary, so that it is a list of data there 
                    dict [Key] .append (Sheet [Key] [Row])
                 the else :
                     for key2 in dict [Key] .keys ():
                         IF of the type (dict [Key] [key2 !]) = of the type (dict [Key]): 
                            dict [Key] [key2] .append (Sheet [Key] [key2] [Row]) 
                        the else :
                             for key3 in dict[key][key2].keys():
                                dict[key][key2][key3].append(sheet[key][key2][key3][row])

Function to achieve:

        def updateDict(tmp, tmp_sheet):
            for key in tmp.keys():
                if type(dict) != type(tmp):
                    continue
                if type(tmp[key]) != type(tmp):
                    if key == "日期":
                        tmp[key].append(tmp_sheet[key])
                    else:
                        tmp[key].append(tmp_sheet[key][row])
                else:
                    updateDict(tmp[key], tmp_sheet[key])
        updateDict(dict,sheet)

 

Guess you like

Origin www.cnblogs.com/pqhuang/p/11326285.html