Search
 
SCRIPT & CODE EXAMPLE
 

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
Comment

PREVIOUS NEXT
Code Example
Python :: ssd 1306 esp32 python 
Python :: python csv file plot column 
Python :: running setup.py install for rjsmin ... error 
Python :: using django annotations to get the last record 
Python :: pandas add mutliple columns 
Python :: python pod status phase 
Python :: Sampling data in different ways 
Python :: python keyerror 0 
Python :: is tkinter built into python 
Python :: python convert docx to pdf linux 
Python :: using ipfn in python 
Python :: why do we need to preprocess data 
Python :: Python - Common Conditional Statements 
Python :: Check if a Key is Already Present in a Dictionary 
Python :: how to filter even or odd model id in django 
Python :: hash tables in python 
Python :: get random bright hex color python 
Python :: dalsports 
Python :: what is require_self 
Python :: command run test keep db python 
Python :: How many handshakes for all the people in your class? python code 
Python :: jhon wick 
Python :: pandas resamples stratified by columns values 
Python :: try finally return precedent 
Python :: python for schleife 
Python :: python empty list boolean 
Python :: ascci value pyth 
Python :: PyQgis Spatial join y attribute 
Python :: python f strings formatting numbers 
Python :: methods accesory python 
ADD CONTENT
Topic
Content
Source link
Name
6+3 =