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