如何从 DeepDiff 中删除特定单词?

问题描述 投票:0回答:1

我需要该代码来比较结构 2-yaml 文件,并且我在使用 DeepDiff 对象时遇到一些问题。

文件1:

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

文件2:

app:
  mainkey:
    key1: 120
    key2: 5

应用程序:

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

输出:

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

预计:

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

如何从 DeepDiff 对象中删除该 ....

root
单词?

python python-3.x dictionary diff python-deepdiff
1个回答
0
投票

这有效:

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])

输出:

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

结果是一个字符串。您可以使用

.split(', ')
将其转换为列表。

© www.soinside.com 2019 - 2024. All rights reserved.