Home > Backend Development > Python Tutorial > How to Remove ANSI Escape Sequences from Python Strings?

How to Remove ANSI Escape Sequences from Python Strings?

Barbara Streisand
Release: 2024-11-23 08:47:11
Original
506 people have browsed it

How to Remove ANSI Escape Sequences from Python Strings?

Stripping ANSI Escape Sequences in Python Strings

Many command-line tools and SSH applications append ANSI escape sequences to their outputs to control terminal behaviors and enhance visual presentation. However, these sequences can be cumbersome when you want to parse or process the string content without visual cues. This article explores a Pythonic approach to remove such escape sequences and extract the plaintext content.

Problem:

Consider the following example string retrieved from an SSH command:

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

The objective is to programmatically remove the ANSI escape sequences, leaving only the plaintext content:

'examplefile.zip'
Copy after login

Solution:

Python's regular expression module provides a succinct solution to this problem. The following regular expression effectively captures and removes all ANSI escape sequences:

import re

ansi_escape = re.compile(r'''
    \x1B  # ESC
    (?:   # 7-bit C1 Fe (except CSI)
        [@-Z\-_]
    |     # or [ for CSI, followed by a control sequence
        \[
        [0-?]*  # Parameter bytes
        [ -/]*  # Intermediate bytes
        [@-~]   # Final byte
    )
''', re.VERBOSE)
Copy after login

To apply the regular expression and extract the desired text:

import re

ansi_escape = re.compile(r'\x1B(?:[@-Z\-_]|\[[0-?]*[ -/]*[@-~])')
sometext = 'ls\r\n\x1b[00m\x1b[01;31mexamplefile.zip\x1b[00m\r\n\x1b[01;31m'
result = ansi_escape.sub('', sometext)
Copy after login

Output:

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

The above is the detailed content of How to Remove ANSI Escape Sequences from Python Strings?. 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