Print / Get full path of every nested item in a dictionary - Python3

Daniel :

I have a multi-layered, nested python dictionary. I am trying to convert this dictionary into a string that looks like human-written python code. I have scoured stackoverflow for a solution but could not find one that fits - please do point one out if it answers my question.

Notes:

1) Some key names are repeated at different depths in the nested dictionary

2) The dictionary itself can change shape, content, and possibly even key names, hence this script should work for different cases

3) To simplify this problem, I converted all nested lists into dictionaries - the keys for these dictionaries are numbers converted to a string-type - however a solution that uses nested lists is okay

The goal is to convert:

sample_dictionary = {
    'id' : '123',
    'type' : 'testing',
    'records' : {
        '0' : {
            'record_id' : '324',
            'record_type' : 'test1',
        },
        '1' : {
            'record_id' : '121',
            'record_type' : 'test2',
            'sub_records' : {
                '0' : {
                    'sub_record_id' : 'sub_578',
                    'sub_record_type' : 'sub_test',
                },
            },
        },
    },
}

into a string:

sample_dictionary[id] = 123
sample_dictionary[type] = testing
sample_dictionary[records][0][record_id] = 324
sample_dictionary[records][0][record_type] = test1
sample_dictionary[records][1][record_id] = 121
sample_dictionary[records][1][record_id] = test2
sample_dictionary[records][1][sub_records][0][sub_record_id] = sub_578
sample_dictionary[records][1][sub_records][0][sub_record_type] = sub_test

I have created this recursive function that gets close, but includes extra keys inappropriately:

def FindValues(dictionary, my_keys=''):

    for key, value in dictionary.items():

        my_keys += '[' + key + ']'

        if type(value) is dict:

            FindValues(value, my_keys)

        else: 

            result = 'sample_dictionary' + my_keys + ' = ' + value

            print(result)


FindValues(sample_dictionary)

resulting in:

sample_dictionary[id] = 123
sample_dictionary[id][type] = testing
sample_dictionary[id][type][records][0][record_id] = 324
sample_dictionary[id][type][records][0][record_id][record_type] = test1
sample_dictionary[id][type][records][0][1][record_id] = 121
sample_dictionary[id][type][records][0][1][record_id][record_type] = test2
sample_dictionary[id][type][records][0][1][record_id][record_type][sub_records][0][sub_record_id] = sub_578
sample_dictionary[id][type][records][0][1][record_id][record_type][sub_records][0][sub_record_id][sub_record_type] = sub_test
kederrac :

you should not accumulate in my_keys variable all the keys, but instead to create a new string base only on the current key from your current dict:

def FindValues(dictionary, my_keys=''):

    for key, value in dictionary.items():

        current_key = my_keys + '[' + key + ']'

        if type(value) is dict:

            FindValues(value, current_key)

        else: 

            result = 'sample_dictionary' + current_key  + ' = ' + value

            print(result)

FindValues(sample_dictionary)

output:

sample_dictionary[id] = 123
sample_dictionary[type] = testing
sample_dictionary[records][0][record_id] = 324
sample_dictionary[records][0][record_type] = test1
sample_dictionary[records][1][record_id] = 121
sample_dictionary[records][1][record_type] = test2
sample_dictionary[records][1][sub_records][0][sub_record_id] = sub_578
sample_dictionary[records][1][sub_records][0][sub_record_type] = sub_test

if you want your function to return a list with your strings you can use:

def FindValues(dictionary, my_keys=''):

    output = []

    for key, value in dictionary.items():

        current_key = my_keys + '[' + key + ']'

        if type(value) is dict:

            output += FindValues(value, current_key)

        else: 

            output.append('sample_dictionary' + current_key  + ' = ' + value)

    return output

FindValues(sample_dictionary)

output:

['sample_dictionary[id] = 123',
 'sample_dictionary[type] = testing',
 'sample_dictionary[records][0][record_id] = 324',
 'sample_dictionary[records][0][record_type] = test1',
 'sample_dictionary[records][1][record_id] = 121',
 'sample_dictionary[records][1][record_type] = test2',
 'sample_dictionary[records][1][sub_records][0][sub_record_id] = sub_578',
 'sample_dictionary[records][1][sub_records][0][sub_record_type] = sub_test']

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=351017&siteId=1