Bạn có thể sử dụng regex để loại bỏ các chuỗi thoát ANSI khỏi chuỗi trong Python. Chỉ cần thay thế các chuỗi thoát bằng chuỗi rỗng bằng re.sub(). Regex bạn có thể sử dụng để loại bỏ các chuỗi thoát ANSI là: r'(\\x9B|\\x1B\\[)[0-?]*[ -/]*[@-~]'.
Ví dụ:
import re
def escape_ansi(line):
ansi_escape = re.compile(r'(\\x9B|\\x1B\\[)[0-?]*[ -/]*[@-~]')
return ansi_escape.sub('', line)
print(escape_ansi(line='\\t\\u001b[0;35mSomeText\\u001b[0m\\u001b[0;36m172.18.0.2\\u001b[0m'))
Điều này sẽ cho ra kết quả:
'\\tSomeText 172.18.0.2'