如何在difflibs html输出中突出显示每行两个以上的字符 [英] How to highlight more than two characters per line in difflibs html output

查看:178
本文介绍了如何在difflibs html输出中突出显示每行两个以上的字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用difflib.HtmlDiff比较两个文件.我希望在输出的html中突出显示差异.

I am using difflib.HtmlDiff to compare two files. I want the differences to be highlighted in the outputted html.

当一行中最多包含两个不同的字符时,这已经起作用:

This already works when there are a maximum of two different chars in one line:

a = "2.000"
b = "2.120"

但是,如果一行上有更多不同的字符,则在输出中,整行都标记为红色(在左侧)或绿色(在表格的右侧):

But when there are more different characters on one line then in the output the whole line is marked red (on the left side) or green (on the right side of the table):

a = "2.000"
b = "2.123"

此行为可以配置吗?那么我可以设置该行被标记为已删除/已添加的不同字符的数量吗?

Is this behaviour configurable? So can I set the number of different characters at which the line is marked as deleted / added?

示例:

import difflib
diff=difflib.HtmlDiff()
print(diff.make_file(
'''
2.000
2.000
2.000
'''.splitlines(),
'''
2.001
2.010
2.011
'''.splitlines()))

给我这个输出:

第2行是我想要的输出.它突出显示了黄色之间的差异. 第3行对我来说很奇怪,因为它没有检测到一个字符更改,而是将其显示为删除/添加. 第4行与第3行相同,但标记了整行.

Line 2 is the output I want. It highlights the differences in yellow. Line 3 is odd for me because it does not detect the one character change but instead shows it as delete / add. Line 4 same as for line 3 but the whole line is marked.

推荐答案

difflib的算法不声称产生最少的编辑顺序. 尽管该声明来自SequenceMatcher的文档,但我怀疑它通常适用于difflib,尤其是HTMLDiff.

difflib's algorithm does not claim to yield minimal edit sequences. Although that statement comes from the docs for SequenceMatcher, I suspect it applies to difflib in general, and HTMLDiff in particular.

在谷歌搜索"python替代difflib最小编辑"时 我找到了 google-diff-match-patch .如果您尝试使用其 Diff演示 用您的示例字符串,它产生

While googling around for "python alternative difflib minimal edit" I found google-diff-match-patch. If you try out their demo for Diff with your example strings, it yields

尽管输出内容与您要求的不完全相同,但它确实表明它发现了最少的编辑内容.

Although the output is not exactly what you requested, it does show that it found the minimal edits.

API文档状态

diff_prettyHtml(diffs)=> html

diff_prettyHtml(diffs) => html

获取一个diff数组并返回一个漂亮的HTML序列.这 函数主要是作为示例来编写自己的函数 显示功能.

Takes a diff array and returns a pretty HTML sequence. This function is mainly intended as an example from which to write ones own display functions.

这表明查看diff_prettyHtml的源代码可能是构建所需HTML表格的良好起点.

which suggests looking at the source code for diff_prettyHtml might be a good starting point from which to build the HTML table you are looking for.

这篇关于如何在difflibs html输出中突出显示每行两个以上的字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆