Search code examples
pythonpython-3.xdictionarypython-deepdiff

Is it possible to exclude a dict path in deepdiff without knowing the keys


I have 2 dicts which I want to get the diff of them. For this I use the deepDiff module which is extremly good. It's possible to add the arg "exclude_paths" but the problem is I want to exclude a Path which I dont know because my dicts are soo big.

Like:

root['thiskeyiknow']['idontknowthiskey']['idontknowthiskey']['iknowthiskey']

so is there a way to exclude all paths which are like this:

root['thiskeyiknow']['everykeyispossiblehere']['everykeyispossiblehere']['thiskeyshouldbeexcluded']

in unix it would be the * which means everything ... is there something in python which says: "here can be anything just put it in?"

I already tried something like this:

root['key1']['']['']['keyiwanttoexclude']

but it didn't work.

So if I have a dict like this:

dict1 = {"key1":{key2:{key3:{key4: "bla"}}}}
dict2 = {"key1":{key2:{key3:{key4: "fasl"}}}}


excludePaths = [

    "root['key1'][all][all]['key4']"

]

diff = deepdiff.DeepDiff(dict1,dict2,exclude_paths=excludePaths)

Output should be:

{}

Output shouldn't be:

{" root['key1']['key2']['key3']['key4']" : {

'new_value' = 'bla'
'old_value' = 'fasl'

}

Solution

  • So I was able to do it.

    Solution:

    Make a list with the exluded regex strings:

    exp:

    excludedRegex = [
         r"root['key1'][.+?][.+?]['key4']"
         etc.
    ]
    

    and then add the excluding list to the deepdict

    diff = deepdiff.DeepDiff(dict1,dict2,exclude_regex_paths=excludeRegex)
    

    and thats all it takes