Home > Backend Development > Python Tutorial > How to Remove ANSI Escape Sequences from a String in Python?

How to Remove ANSI Escape Sequences from a String in Python?

Susan Sarandon
Release: 2024-11-28 04:38:11
Original
358 people have browsed it

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)
Copy after login

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)
Copy after login

Example:

Given the following string:

ls\r\n\x1b[00m\x1b[01;31mexamplefile.zip\x1b[00m\r\n\x1b[01;31m
Copy after login

Using the above regular expression, you obtain the cleaned string:

ls\r\nexamplefile.zip\r\n
Copy after login

Additional Notes:

  • The regular expression covers all 7-bit ANSI C1 escape sequences, but not the 8-bit C1 escape sequence openers.
  • If you need to handle 8-bit codes, a slightly more complex regular expression is required.
  • For more information on ANSI escape codes, refer to the ANSI escape codes overview on Wikipedia or the ECMA-48 standard.

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template