difflib opcodes
Let's say you want to find the differences in two strings, but want to ignore spaces. difflib can do that
texta = "this is a small test"
textb = "this isa small text"
from difflib import SequenceMatcher
def print_replacements(texta, textb):
sm = SequenceMatcher(a=texta, b=textb)
for op, a_start, a_end, b_start, b_end in sm.get_opcodes():
frag_a = texta[a_start:a_end]
frag_b = textb[b_start:b_end]
a_pre = a_start - 1 if a_start > 0 else 0
b_pre = b_start - 1 if b_start > 0 else 0
a_post = a_end + 1 if a_end < (len(texta) - 1) else a_end
b_post = b_end + 1 if b_end < (len(textb) - 1) else b_end
if op == "equal":
continue
elif op == "delete":
if frag_a == " ":
continue
print(f"del\t{texta[a_pre:a_post]}\t{textb[b_pre:b_post]}")
elif op == "insert":
if frag_b == " ":
continue
print(f"ins\t{texta[a_pre:a_post]}\t{textb[b_pre:b_post]}")
elif op == "replace":
print(f"repl\t{frag_a}\t{frag_b}")
print_replacements(texta, textb)