在 Python 中生成漂亮的 diff HTML
我有两块文本,我想比较并查看在 Python 中添加/删除/修改了哪些单词/行(类似于 Wiki 的 Diff 输出)。
我尝试过 difflib.HtmlDiff 但它的输出不太漂亮。
Python(或外部库)中是否有一种方法可以生成两组文本块的差异的干净的 HTML? (不仅是行级别,还包括行内的单词/字符修改)
I have two chunks of text that I would like to compare and see which words/lines have been added/removed/modified in Python (similar to a Wiki's Diff Output).
I have tried difflib.HtmlDiff but it's output is less than pretty.
Is there a way in Python (or external library) that would generate clean looking HTML of the diff of two sets of text chunks? (not just line level, but also word/character modifications within a line)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
diff-match-patchdiff_prettyHtml() > 来自 Google 的库。
There's
diff_prettyHtml()
in the diff-match-patch library from Google.一般来说,如果您希望某些 HTML 以更漂亮的方式呈现,您可以通过添加 CSS 来实现。
例如,如果您像这样生成 HTML:
那么您将在添加的行上获得绿色背景,在更改的行上获得黄色背景,在删除时获得红色背景。如果我这样做,我会获取生成的 HTML,提取正文,并在其前面加上我自己手写的 HTML 块和大量 CSS,以使其看起来不错。我也可能会删除图例表并将其移至顶部或将其放入 div 中,以便 CSS 可以做到这一点。
实际上,我会认真考虑修复 difflib 模块(用 python 编写)以生成更好的 HTML 并将其贡献回项目。如果您有 CSS 专家来帮助您或者您自己就是专家,请考虑这样做。
Generally, if you want some HTML to render in a prettier way, you do it by adding CSS.
For instance, if you generate the HTML like this:
then you get green backgrounds on added lines, yellow on changed lines and red on deleted. If I were doing this I would take take the generated HTML, extract the body, and prefix it with my own handwritten block of HTML with lots of CSS to make it look good. I'd also probably strip out the legend table and move it to the top or put it in a div so that CSS can do that.
Actually, I would give serious consideration to just fixing up the difflib module (which is written in python) to generate better HTML and contribute it back to the project. If you have a CSS expert to help you or are one yourself, please consider doing this.
我最近发布了一个 python 脚本,它就是这样做的:diff2HtmlCompare(点击链接查看屏幕截图)。在底层,它包装了 difflib 并使用 pygments 进行语法突出显示。
I recently posted a python script that does just this: diff2HtmlCompare (follow the link for a screenshot). Under the hood it wraps difflib and uses pygments for syntax highlighting.
xmldiff 似乎是一个不错的包这个目的尤其是当您要比较 XML/HTML 时。请参阅他们的文档了解更多信息。
xmldiff seems to be a nice package for this purpose especially when you have XML/HTML to compare. Read more in their documentation.
由于来自 google 的 .. 库似乎不再有积极的开发,我建议使用 diff_py
来自github页面:
Since the .. library from google seems to have no active development any more, I suggest to use diff_py
From the github page:
首先尝试通过 lxml.html 清理 HTML,然后通过 difflib 检查差异
try first of all clean up both of HTML by lxml.html, and the check the difference by difflib
我自己的答案副本来自此处 。
DaisyDiff 怎么样 (Java 和 PHP 版本可用的)。
以下功能非常好:
A copy of my own answer from here.
What about DaisyDiff (Java and PHP vesions available).
Following features are really nice: