In the unlikely event that you have xterm256 color codes, this will filter both the "normal" ansi and xterm256 ansi codes:
import re print re.sub(r'\x1b(\[.*?[@-~]|\].*?(\x07|\x1b\\))', '', a)
or in slightly less obfuscation and more readable form:
'(' + CSI + '.*?' + CMD + '|' + OSC + '.*?' + '(' + ST + '|' + BEL + ')' + ')'
Full code with tests:
import re tests = [ u"22200K .......\u001b[0m\u001b[91m... .......... ...\u001b[0m\u001b[91m.\u001b[0m\u001b[91m...... .........\u001b[0m\u001b[91m.\u001b[0m\u001b[91m \u001b[0m\u001b[91m.\u001b[0m\u001b[91m.\u001b[0m\u001b[91m.\u001b[0m\u001b[91m.\u001b[0m\u001b[91m...... 50% 28.6K 12m55s", u"=\u001b[m=", u"-\u001b]23\u0007-", ] def trim_ansi(a): ESC = r'\x1b' CSI = ESC + r'\[' OSC = ESC + r'\]' CMD = '[@-~]' ST = ESC + r'\\' BEL = r'\x07' pattern = '(' + CSI + '.*?' + CMD + '|' + OSC + '.*?' + '(' + ST + '|' + BEL + ')' + ')' return re.sub(pattern, '', a) for t in tests: print trim_ansi(t)
source share