实验管理——Config参数值对比的学习笔记

1 Difflib.Differ: 可以实现配置文本的对比

difflib有一点很棒的地方在于,可以在对比多行文本时,获得人类易读的对比结果,这一点是在 difflib doc 中看到的:

[difflib.SequenceMatcher]: This is a flexible class for comparing pairs of sequences of any type, so long as the sequence elements are hashable. The basic algorithm predates, and is a little fancier than, an algorithm published in the late 1980’s by Ratcliff and Obershelp under the hyperbolic name “gestalt pattern matching.” The idea is to find the longest contiguous matching subsequence that contains no “junk” elements; these “junk” elements are ones that are uninteresting in some sense, such as blank lines or whitespace. (Handling junk is an extension to the Ratcliff and Obershelp algorithm.) … This does not yield minimal edit sequences, but does tend to yield matches that “look right” to people.

示例代码:

a = ["a=1","b=2"]
b = ["a=2","c=3","b=1"]
text1 = '''a=1
    b=2
'''.splitlines(keepends=True)
text2 = '''a=2
    c=3
    b=1
'''.splitlines(keepends=True)
d = Differ()
list(d.compare(text1, text2))
>>> ['- a=1\n',
	 '?   ^\n',	# ?表示该行是内容差异的注释
	 '+ a=2\n',
	 '?   ^\n',
	 '+     c=3\n',
	 '-     b=2\n',
	 '?       ^\n',
	 '+     b=1\n',
	 '?       ^\n']
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值