Creating a History View of Text in Python

I have two versions of a piece of text, and I want to create an HTML version of its revision, similar to what Google Docs or Stack Overflow displays. I need to do this in Python. I don't know how this method is called, but I am assuming it has a name and hopefully there is a Python library that can do this.

Version 1:

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

Version 2:

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

Desired output:

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

Using the diff command doesn't work because it tells me which lines are different, but not which columns / words are different.

$ 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

      

+2


a source to share


3 answers


Google Diff Merge Patch has a pretty good diff implementation in pure python.



+1


a source


You can use wdiff . I don't know if there is a Python implementation:



$ 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.+}

      

0


a source


The difflib module can help solve this problem.

0


a source







All Articles