[Python] Compare 2 nested dicts ignoring whitespace in strings

Discussão em 'Python' iniciado por Stack, Abril 3, 2025.

  1. Stack

    Stack Membro Participativo

    I want to compare two nested python dicts and ignore whitespace in the values. In the following example a compare function should return true as d1 and d2 are equal except for whitespace.

    d1 = {
    'key1': 'value1',
    'key2': {
    'key2_1': 'value2_1',
    'key2_2': 'value2_2',
    'key2_3': {
    'key2_3_1': 'value2_3_1'
    }
    }
    }

    d2 = {
    'key1': 'val ue1',
    'key2': {
    'key2_1': 'val \n ue2_1',
    'key2_2': 'value2_2',
    'key2_3': {
    'key2_3_1': 'value2_3_1\n'
    }
    }
    }


    Is there a build-in way to do it or do I have to recurse through all levels and compare the strings?

    Continue reading...

Compartilhe esta Página