Created
January 19, 2023 06:24
-
-
Save laixintao/a412a300e1e91b3f0bfb205009442d1c to your computer and use it in GitHub Desktop.
remove console code and ansi escape code using Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def escape_ansi(line): | |
# 7-bit and 8-bit C1 ANSI sequences | |
ansi_escape_8bit = re.compile( | |
rb""" | |
(?: # either 7-bit C1, two bytes, ESC Fe (omitting CSI) | |
\x1B | |
[@-Z\\-_] | |
| # or a single 8-bit byte Fe (omitting CSI) | |
[\x80-\x9A\x9C-\x9F] | |
| # or CSI + control codes | |
(?: # 7-bit CSI, ESC [ | |
\x1B\[ | |
| # 8-bit CSI, 9B | |
\x9B | |
) | |
[0-?]* # Parameter bytes | |
[ -/]* # Intermediate bytes | |
[@-~] # Final byte | |
| \x1b. | |
) | |
""", | |
re.VERBOSE, | |
) | |
result = ansi_escape_8bit.sub(b"", line) | |
return result |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment