
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
How can I remove the ANSI escape sequences from a string in python?
You can use regexes to remove the ANSI escape sequences from a string in Python. Simply substitute the escape sequences with an empty string using re.sub(). The regex you can use for removing ANSI escape sequences is: '(\x9B|\x1B\[)[0-?]*[ -\/]*[@-~]'.
For example,
import re def escape_ansi(line): ansi_escape =re.compile(r'(\x9B|\x1B\[)[0-?]*[ -\/]*[@-~]') return ansi_escape.sub('', line) print escape_ansi(line = '\t\u001b[0;35mSomeText\u001b[0m\u001b[0;36m172.18.0.2\u001b[0m')
This will give the output:
'\tSomeText 172.18.0.2'
- Related Articles
- How to process escape sequences in a string in Python?
- How do I un-escape a backslash-escaped string in Python?
- Escape sequences in Java
- Escape sequences in C
- How do I remove a substring from the end of a string in Python?
- How can I remove Python from my Windows Machine?
- How to Remove Punctuations From a String in Python?
- How can I escape HTML special chars in JavaScript?
- Remove Vowels from a String in Python
- What are the escape sequences supported by C#?
- How can I remove a value from an enum in MySQL?
- How to remove specific characters from a string in Python?
- How can I remove dandruff from my hair?
- How do I remove a string from an array in a MongoDB document?
- How can I remove all empty values when I explode a string using PHP?

Advertisements