Skip to content Skip to sidebar Skip to footer

Finding Differences Between Strings

I have the following function that gets a source and a modified strings, and bolds the changed words in it. def appendBoldChanges(s1, s2): 'Adds tags to wor

Solution 1:

You could use difflib, and do it like this:

from difflib import Differ

def appendBoldChanges(s1, s2):
    "Adds <b></b> tags to words that are changed"
    l1 = s1.split(' ')
    l2 = s2.split(' ')
    dif = list(Differ().compare(l1, l2))
    return " ".join(['<b>'+i[2:]+'</b>' if i[:1] == '+' else i[2:] for i in dif 
                                                           if not i[:1] in '-?'])

print appendBoldChanges("britney spirs", "britney sprears")
print appendBoldChanges("sora iro days", "sorairo days")
#Output:
britney <b>sprears</b>
<b>sorairo</b> days

Solution 2:

Have a look at the difflib module, you could use a SequenceMatcher to find the changed regions in your text.

Solution 3:

A small upgrade tp @fraxel answer that returns 2 outputs - the original and the new version with marked changes. I also change the one-liner to a more readable version in my opinion

def show_diff(text, n_text):
    seqm = difflib.SequenceMatcher(None, text, n_text)
    output_orig = []
    output_new = []
    for opcode, a0, a1, b0, b1 in seqm.get_opcodes():
        orig_seq = seqm.a[a0:a1]
        new_seq = seqm.b[b0:b1]
        if opcode == 'equal':
            output_orig.append(orig_seq)
            output_new.append(orig_seq)
        elif opcode == 'insert':
            output_new.append("<font color=green>{}</font>".format(new_seq))
        elif opcode == 'delete':
            output_orig.append("<font color=red>{}</font>".format(orig_seq))
        elif opcode == 'replace':
            output_new.append("<font color=blue>{}</font>".format(new_seq))
            output_orig.append("<font color=blue>{}</font>".format(orig_seq))
        else:
            print('Error')
    return ''.join(output_orig), ''.join(output_new)

Post a Comment for "Finding Differences Between Strings"