Skip to content

Instantly share code, notes, and snippets.

@TheLonelyGhost
Created September 9, 2021 07:40
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 TheLonelyGhost/8f6477fbf10a94f2a8e29b03371ca805 to your computer and use it in GitHub Desktop.
Save TheLonelyGhost/8f6477fbf10a94f2a8e29b03371ca805 to your computer and use it in GitHub Desktop.
Remove all ANSI escape sequences from a given string
#!/usr/bin/env python3
import fileinput
import re
# 7-bit C1 ANSI sequences
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)
for line in fileinput.input():
if 'Exit' == line.rstrip():
break
print(ansi_escape.sub('', line.rstrip()))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment