ANSI转义序列
转义是当由于技术等原因、无法直接在代码中写出所要的字符时采用的,以多个字符的有序组合来表示原本需要的字符的手段,而转义序列(英语:escape sequence)指在转义时使用的有序字符组合。
转译字符
有些字符具有特殊含义,必须特殊对待。
常见的转译字符
\n 換行符號
\r 回行符號
\t 定位 tab 符號
\f 跳頁
\b 退格
\a 鈴聲
\0 八進位數,如:\007
\x 十六進位數,如:\x16
\ \ 這個符號
\” ” 雙引號
用”\r”举例
s = "12345"
print s # 12345
r = "123\r45"
print r # 453,在\r处跳转到行首
转译序列
ANSI sequences generally 通常格式为:
ESC [ ; …
转译说明
ESC [ 0 m # reset all (colors and brightness)
ESC [ 1 m # bright
ESC [ 2 m # dim (looks same as normal brightness)
ESC [ 22 m # normal brightnessFOREGROUND:
ESC [ 30 m # black
ESC [ 31 m # red
ESC [ 32 m # green
ESC [ 33 m # yellow
ESC [ 34 m # blue
ESC [ 35 m # magenta
ESC [ 36 m # cyan
ESC [ 37 m # white
ESC [ 39 m # resetBACKGROUND
ESC [ 40 m # black
ESC [ 41 m # red
ESC [ 42 m # green
ESC [ 43 m # yellow
ESC [ 44 m # blue
ESC [ 45 m # magenta
ESC [ 46 m # cyan
ESC [ 47 m # white
ESC [ 49 m # resetcursor positioning
ESC [ y;x H # position cursor at x across, y down
ESC [ y;x f # position cursor at x across, y down
ESC [ n A # move cursor n lines up
ESC [ n B # move cursor n lines down
ESC [ n C # move cursor n characters forward
ESC [ n D # move cursor n characters backwardclear the screen
ESC [ mode J # clear the screen
clear the line
ESC [ mode K # clear the line
这里ESC可以用16进制\x1b表示,也可以用8进制\033表示。
s = "hello world\r\x1b[Kansi"
print s #ansi,跳转到行首,然后清理一行
相关博文
https://jcastellssala.com/2012/07/20/python-command-line-waiting-feedback-and-some-background-on-why/
http://www.jianshu.com/p/a924a6d6ed22