Python difflib:内联突出显示差异? [英] Python difflib: highlighting differences inline?
本文介绍了Python difflib:内联突出显示差异?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在比较相似的行时,我要强调同一行的区别:
a)lorem ipsum dolor sit amet
b)lorem foo ipsum dolor amet
lorem< ins> foo< / ins> ipsum dolor< del> sit< / del> amet
虽然difflib.HtmlDiff似乎进行了这种内联突出显示,但它会产生非常冗长的标记。 / p>
不幸的是,我无法找到另一种不能逐行操作的类/方法。
我错过了什么吗?
任何指针将不胜感激!
解决方案
举个简单的例子:
import difflib
def show_diff(seqm):
两个比较字符串之间的统一操作
seqm是difflib.SequenceMatcher实例其a和b是字符串
output = []
用于seqm.get_opcodes()中的操作码a0,a1,b0,b1:如果操作码=='等于',则
:
output.append(seqm.a [a0:a1])$ b $ b elif操作码=='插入':
output.append(< ins> + seqm.b [b0 :b1] +< / ins>)
elif操作码=='删除':
output.append(< del> + seqm.a [a0:a1] +< ; / del>)
elif opcode =='replace':
引发NotImplementedError,与'replace'操作码有什么关系?
else:
引发RuntimeError,意外操作码
返回’。join(output)
>> sm = difflib.SequenceMatcher(无, lorem ipsum dolor amet, lorem foo ipsum dolor amet)
>> show_diff(sm)
’lorem< ins> foo< / ins> ipsum dolor< del> sit< / del> amet'
这适用于字符串。您应该决定如何处理替换操作码。
When comparing similar lines, I want to highlight the differences on the same line:
a) lorem ipsum dolor sit amet
b) lorem foo ipsum dolor amet
lorem <ins>foo</ins> ipsum dolor <del>sit</del> amet
While difflib.HtmlDiff appears to do this sort of inline highlighting, it produces very verbose markup.
Unfortunately, I have not been able to find another class/method which does not operate on a line-by-line basis.
Am I missing anything? Any pointers would be appreciated!
解决方案
For your simple example:
import difflib
def show_diff(seqm):
"""Unify operations between two compared strings
seqm is a difflib.SequenceMatcher instance whose a & b are strings"""
output= []
for opcode, a0, a1, b0, b1 in seqm.get_opcodes():
if opcode == 'equal':
output.append(seqm.a[a0:a1])
elif opcode == 'insert':
output.append("<ins>" + seqm.b[b0:b1] + "</ins>")
elif opcode == 'delete':
output.append("<del>" + seqm.a[a0:a1] + "</del>")
elif opcode == 'replace':
raise NotImplementedError, "what to do with 'replace' opcode?"
else:
raise RuntimeError, "unexpected opcode"
return ''.join(output)
>>> sm= difflib.SequenceMatcher(None, "lorem ipsum dolor sit amet", "lorem foo ipsum dolor amet")
>>> show_diff(sm)
'lorem<ins> foo</ins> ipsum dolor <del>sit </del>amet'
This works with strings. You should decide what to do with "replace" opcodes.
这篇关于Python difflib:内联突出显示差异?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文