Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

python show difference between two strings and colorize it

import difflib

red = lambda text: f"33[38;2;255;0;0m{text}33[38;2;255;255;255m"
green = lambda text: f"33[38;2;0;255;0m{text}33[38;2;255;255;255m"
blue = lambda text: f"33[38;2;0;0;255m{text}33[38;2;255;255;255m"
white = lambda text: f"33[38;2;255;255;255m{text}33[38;2;255;255;255m"

def get_edits_string(old, new):
    result = ""
    codes = difflib.SequenceMatcher(a=old, b=new).get_opcodes()
    for code in codes:
        if code[0] == "equal": 
            result += white(old[code[1]:code[2]])
        elif code[0] == "delete":
            result += red(old[code[1]:code[2]])
        elif code[0] == "insert":
            result += green(new[code[3]:code[4]])
        elif code[0] == "replace":
            result += (red(old[code[1]:code[2]]) + green(new[code[3]:code[4]]))
    return result
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #python #show #difference #strings #colorize
ADD COMMENT
Topic
Name
3+4 =