用Python生成文本修订历史的视图

2021-04-11 22:54:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一段文本的两个版本,我想生成一个类似于googledocs或Stack Overflow显示内容的HTML视图。我需要用Python来做这个。我不知道这项技术叫什么,但我假设它有一个名字,希望有一个Python库可以做到。在

版本1:

William Henry "Bill" Gates III (born October 28, 1955)[2] is an American business magnate, philanthropist, and chairman[3] of Microsoft, the software company he founded with Paul Allen.

版本2:

William Henry "Bill" Gates III (born October 28, 1955)[2] is a business magnate, philanthropist, and chairman[3] of Microsoft, the software company he founded with Paul Allen. He is American.

所需输出:

William Henry "Bill" Gates III (born October 28, 1955)[2] is an American business magnate, philanthropist, and chairman[3] of Microsoft, the software company he founded with Paul Allen. He is American.

使用diff命令不起作用,因为它告诉我哪些行不同,而不是哪些列/单词不同。在

$ echo 'William Henry "Bill" Gates III (born October 28, 1955)[2] is an American business magnate, philanthropist, and chairman[3] of Microsoft, the software company he founded with Paul Allen.' > oldfile
$ echo 'William Henry "Bill" Gates III (born October 28, 1955)[2] is a business magnate, philanthropist, and chairman[3] of Microsoft, the software company he founded with Paul Allen.  He is American.' > newfile
$ diff -u oldfile newfile
--- oldfile 2010-04-30 13:32:43.000000000 -0700
+++ newfile 2010-04-30 13:33:09.000000000 -0700
@@ -1 +1 @@
-William Henry "Bill" Gates III (born October 28, 1955)[2] is an American business magnate, philanthropist, and chairman[3] of Microsoft, the software company he founded with Paul Allen.
+William Henry "Bill" Gates III (born October 28, 1955)[2] is a business magnate, philanthropist, and chairman[3] of Microsoft, the software company he founded with Paul Allen.  He is American.' > oldfile
3条回答
网友
1楼 ·

您可以使用wdiff。我不知道是否有Python实现:

$ wdiff oldfile newfile
William Henry "Bill" Gates III (born October 28, 1955)[2] is [-an American-] {+a+} business magnate, philanthropist, and chairman[3] of Microsoft, the software company he founded with Paul Allen.  {+He is American.+}
网友
2楼 ·

Google Diff Merge Patch在纯python中有一个非常好的diff实现。在

网友
3楼 ·

difflib模块可能有助于解决此问题。在

相关问题