Home >Backend Development >Python Tutorial >How to Remove ANSI Escape Sequences from a String in Python?
Removing ANSI Escape Sequences from a String in Python
Problem:
You have a string that contains ANSI escape sequences, such as those used for color highlighting in SSH commands. These sequences interfere with your program's functionality, and you need to remove them to obtain the underlying text.
Solution:
One effective way to remove ANSI escape sequences from a string is to use a regular expression. Python's re module provides the sub() method, which can substitute the matched sequences with an empty string.
import re ansi_escape = re.compile(r'\x1B(?:[@-Z\-_]|\[[0-?]*[ -/]*[@-~])') cleaned_string = ansi_escape.sub('', original_string)
This regular expression matches all 7-bit ANSI C1 escape sequences. It captures either a two-byte sequence starting with an escape character (ESC) followed by a control character or a single 8-bit byte representing the control character. By replacing these matches with an empty string, the sub() method effectively removes the ANSI sequences.
Condensed Version:
cleaned_string = re.sub(r'\x1B(?:[@-Z\-_]|\[[0-?]*[ -/]*[@-~])', '', original_string)
Example:
Given the following string:
ls\r\n\x1b[00m\x1b[01;31mexamplefile.zip\x1b[00m\r\n\x1b[01;31m
Using the above regular expression, you obtain the cleaned string:
ls\r\nexamplefile.zip\r\n
Additional Notes:
The above is the detailed content of How to Remove ANSI Escape Sequences from a String in Python?. For more information, please follow other related articles on the PHP Chinese website!