Home > OS >  Trying to get a good print out of super nested dictionaries in Python
Trying to get a good print out of super nested dictionaries in Python

Time:03-05

I have inherited code that I want to get a good print out of what is going on. It's sort of like what is shown in Variables window in VS Code while debugging but more easy to read since the nested dictionaries are random and large

Issue:

I have this dictionary that has so many nested dictionaries and values that I am trying to make sense of. The base is the results variable that I need to see in greater/easy to read detail.

A sample of results is like so:

# Example
results['dataPartA'] = {}

# Sample of random data in `dataPartA`
results['dataPartA'] has two dictionaries with keys 1 and 2 and they each can look like:

# `dataPartA` dictionary with key = 1:
Key Value
(1,1)   17.9
(1,2)   12.4
(1,3)   67.3

# `dataPartA` dictionary with key = 2:
Key Value
(2,1)   4.5
(2,2)   14.6
(2,3)   61.3

Trying to get

There are like 25 of these main keys like dataPartA. I need the output to be something like this so it is readable:

Output
dataPartA
    1
        (1,1)   17.9
        (1,2)   12.4
        (1,3)   67.3
    2
        (2,1)   4.5
        (2,2)   14.6
        (2,3)   61.3
anotherDictionaryInResults
    etc.

Tried

# This is test code to help with debugging the dictionaries
def all_keys(self, dict_obj):

        # Iterate over all keys of the dictionary
        for key , value in dict_obj.items():
            yield key
            # If value is of dictionary type then yield all keys
            # in that nested dictionary
            if isinstance(value, dict):
                for k in self.all_keys(value):
                    yield k
            else:
                yield value

def figure_out_function(self, results):

    for key in self.all_keys(results):
        print ('Testing')
        print(key)

This prints everything out line by line and the terminal and it appears the terminal cuts off since there is so many print out lines. Any good ideas?

CodePudding user response:

How about the following, where we introduce a new state variable level:

results = {
    'A': {1: {(1, 1): 17.9, (1, 2): 12.4, (1, 3): 67.3},
          2: {(2, 1): 4.5,  (2, 2): 14.6, (2, 3): 61.3}},
    'B': {1: {(1, 1): 17.9, (1, 2): 12.4, (1, 3): 67.3},
          2: {(2, 1): 4.5,  (2, 2): 14.6, (2, 3): 61.3}}}

def with_indent(dct, level=0):
    indent = '    ' * level
    for key, val in dct.items():
        if isinstance(val, dict):
            yield f"{indent}{key}"
            yield from with_indent(val, level   1)
        else:
            yield f"{indent}{key} {val}"

for line in with_indent(results):
    print(line)

Output:

A
    1
        (1, 1) 17.9
        (1, 2) 12.4
        (1, 3) 67.3
    2
        (2, 1) 4.5
        (2, 2) 14.6
        (2, 3) 61.3
B
    1
        (1, 1) 17.9
        (1, 2) 12.4
        (1, 3) 67.3
    2
        (2, 1) 4.5
        (2, 2) 14.6
        (2, 3) 61.3
  • Related