Search code examples
pythondictionarypathnestedordereddictionary

Print all paths to the keys and values in a nested OrderedDict with nested arrays


I'm trying to get the path from nested data structures with arrays and OderedDicts. The problem is that the function i found here: Find a given key's value in a nested ordered dict python will not work with arrays in it.

I've been trying this with Python version 3.7.3 on windows Environment.

This is how i would like it, but then with arrays:

from collections import OrderedDict

mydict = OrderedDict ( {'a':
            OrderedDict ( {'b':
                OrderedDict ( [ ('chart_layout', '3'),
                 ('client_name', 'Sport Parents (Regrouped)'),
                 ('sort_order', 'asending'),
                 ('chart_type', 'pie'),
                 ('powerpoint_color', 'blue'),
                 ('crossbreak', 'Total')
                 ] ) } ) } )

def listRecursive (d, path = None):
    if not path: path = []
    for k, v in d.items ():
        if isinstance (v, OrderedDict):
            for path, found in listRecursive (v, path + [k] ):
                yield path, found
        if isinstance (v, str):
            yield path + [k], v

for path, found in listRecursive (mydict):
    print (path, found)

Output:

['a', 'b', 'chart_layout'] 3
['a', 'b', 'client_name'] Sport Parents (Regrouped)
['a', 'b', 'sort_order'] asending
['a', 'b', 'chart_type'] pie
['a', 'b', 'powerpoint_color'] blue
['a', 'b', 'crossbreak'] Total

This collection is not the actual one. Its more nested with arrays.


xml_order_dict = OrderedDict([('breakfast_menu',
                               OrderedDict([('food',
                                [OrderedDict([('name', 'Belgian Waffles'),
                                              ('price', '$5.95'),
                                              ('description',
                                               'Two of our famous Belgian Waffles '
                                               'with plenty of real maple syrup'),
                                              ('calories', '650')]),
                                 OrderedDict([('name',
                                           'Strawberry Belgian Waffles'),
                                              ('price', '$7.95'),
                                              ('description',
                                               'Light Belgian waffles covered with '
                                               'strawberries and whipped cream'),
                                              ('calories', '900')
                                             ])])]))])
def ListTags(d, key):
    for k, v in d.items ():
        if isinstance (v, OrderedDict):
            for found in listRecursive (v, key):
                yield found
        if k == key:
            yield v

for found in ListTags(xml_order_dict):
    print (found)

The expected results are: Path to tag the result of the tag

input:

for found in ListTags(xml_order_dict):
    print (found)

Output: path = result

breakfast_menu['breakfast_menu']['food'][0]['name'] = Belgian Waffles
breakfast_menu['breakfast_menu']['food'][0]['price'] = $5.95
....

last one of the output:

breakfast_menu['breakfast_menu']['food'][1]['calories'] = 900

Excuse my English, I'm not a native English speaker.


Solution

  • Try this function:

    def list_recursive(mydict, path=()):
        if type(mydict) is list:
            for i, item in enumerate(mydict):
                list_recursive(item, path=(*path, i))
            return
        for k, v in mydict.items():
            if type(v) is str:
                print(*map(
                    lambda x:f"['{x}']" if type(x) is str else f"[{x}]",
                    (*path, k)
                ), '=', v, sep='')
            else:
                list_recursive(v, path=(*path, k))
    

    If you are doing this to generate the code that can recreate the list, consider looking into json formatting instead.