Python uses the RE (Regular Expression) module to filter the Telnet font color

Source: Internet
Author: User

Reason

The telnet protocol supports the use of escape characters to enhance customer display and other special effects. However, the client program using the Telnet protocol has a great deal of interference in reading and analyzing data. How can this problem be filtered out?

Implementation

The telnet mode has ANSI escape characters starting with ESC, which can easily control character color, background and other attributes. Common color escape characters:

The color corresponds to the Telnet ANSI character
Red ESC [31 m
Green ESC [32 m
Yellow ESC [33 m
Blue ESC [34 m
Zihong ESC [35 m
Blue-green ESC [36 m
Black (white) ESC [37 m
Reset ESC [m

It is found that the code is followed by ESC (/x16) and ended with M. With this mode, you can use the re regular expression to facilitate filtering.

Source code

Def _ zyresult (self, S ):
Re. Compile ('(/x1b/[[0-9] * m )')
Return P. sub ('', S)

Tested to fully meet the requirements, O (∩ _ ∩) o...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.