Skip to content

Instantly share code, notes, and snippets.

@laixintao
Created January 19, 2023 06:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save laixintao/a412a300e1e91b3f0bfb205009442d1c to your computer and use it in GitHub Desktop.
Save laixintao/a412a300e1e91b3f0bfb205009442d1c to your computer and use it in GitHub Desktop.
remove console code and ansi escape code using Python
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