我需要该代码来比较结构 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
单词?
这有效:
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(', ')
将其转换为列表。