Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In python, produce HTML highlighting the differences of two simple strings

I need to highlight the differences between two simple strings with python, enclosing the differing substrings in a HTML span attribute. So I'm looking for a simple way to implement the function illustrated by the following example:

hightlight_diff('Hello world','HeXXo world','red')

...it should return the string:

'He<span style="color:red">XX</span>o world'

I have googled and seen difflib mentioned, but it's supposed to be obsolete and I haven't found any good simple demo.

like image 898
user1069609 Avatar asked Feb 22 '12 13:02

user1069609


1 Answers

Everything that you need comes out of difflib -- for example:

>>> import difflib
>>> d = difflib.Differ()
>>> l = list(d.compare("hello", "heXXo"))
>>> l
['  h', '  e', '- l', '- l', '+ X', '+ X', '  o']

Each element in that list is a character from your two input strings, prefixed with one of

  • " " (2 spaces), character present at that position in both strings
  • "- " (dash space), character present at that position in the first string
  • "+ " (plus space), character present at that position in the second string.

Iterate through that list and you can build exactly the output you're looking to create.

There's no mention of difflib being in any way obsolete or deprecated in the docs.

like image 64
bgporter Avatar answered Oct 05 '22 23:10

bgporter