如何从 DeepDiff 中删除特定单词?
How to delete specific word from DeepDiff?
我需要该代码来编译结构 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(', ')
.
将其转换为列表
我需要该代码来编译结构 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(', ')
.