这些是
ANSI escape codes
.我想你的指挥部正试图用粗体字打印一些东西(
[1m
),第一个字符为青色(
[36m
),然后使用
[0m
的来清除格式。尝试删除序列:
注释
-使用Ubuntu 20.04和Python 3.8进行测试
import re
import pexpect
test = b'\x1b[32mwhich ls: /usr/bin/ls\x1b[0m\r\n\x1b[32mwhoami: stack\x1b[0m\r\n\x1b[31;1mCannot execute command find foo: /usr/bin/find: \xe2\x80\x98foo\xe2\x80\x99: No such file or directory\x1b[0m\r\n\x1b[32mdate: Sun 30 Jan 2022 11:53:37 PM EST\x1b[0m\r\n'
# Show raw output
print(test)
# Show formatted output
print(test.decode('utf-8'))
# Remove ANSI escape sequences and print
remove_ansi = re.compile(r'(\x9B|\x1B\[)[0-?]*[ -/]*[@-~]')
print(remove_ansi.sub('', str(test.decode('utf-8'))))
# Your script goes here
command_output = pexpect.run('[command')
print(remove_ansi.sub('', str(command_output.decode('utf-8'))))