ANSI转义序列

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 brightness

FOREGROUND:

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 # reset

BACKGROUND

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 # reset

cursor 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 backward

clear 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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值