Created
September 9, 2021 07:40
-
-
Save TheLonelyGhost/8f6477fbf10a94f2a8e29b03371ca805 to your computer and use it in GitHub Desktop.
Remove all ANSI escape sequences from a given string
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
#!/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