pythonpython-3.xdictionarydiffpython-deepdiff

How to delete specific word from DeepDiff?


I need that code for compate of structure 2-yaml files, And I have some trouble with DeepDiff object.

File1:

app:
  mainkey:
    key1: 60
    key2: 5
  mainkey2:
    key1: 120
    key2: 5
  mainkey3:
    test: value

File2:

app:
  mainkey:
    key1: 120
    key2: 5

app:

import yaml
from deepdiff import DeepDiff

with open("file1.yaml", "r") as f1:
        f1 = f1.read()
        f1 = yaml.safe_load(f1)
with open("file2.yaml", "r") as f2:
        f2 = f2.read()
        f2 = yaml.safe_load(f2)

diffs = DeepDiff(f1, f2)
print(diffs['dictionary_item_removed'])
print(type(diffs['dictionary_item_removed']))

$ python3 app.py

Output:

[root['app']['mainkey2'], root['app']['mainkey3']]
<class 'deepdiff.model.PrettyOrderedSet'>

Expect: ['app']['mainkey2'], ['app']['mainkey3']

How to delete that .... root word from DeepDiff object?


Solution

  • This works:

    import yaml
    from deepdiff import DeepDiff
    
    with open("file1.yaml", "r") as f1:
            f1 = f1.read()
            f1 = yaml.safe_load(f1)
    with open("file2.yaml", "r") as f2:
            f2 = f2.read()
            f2 = yaml.safe_load(f2)
    
    diffs = DeepDiff(f1, f2)
    print(str(diffs['dictionary_item_removed']).replace('root','')[1:-1])
    

    Output:

    ['app']['mainkey3'], ['app']['mainkey2']
    

    The result is a string. You can convert it into a list by using .split(', ').