有没有办法在python中找到(甚至是最好的猜测)字符串的“打印”长度?例如,'potaa\bto‘在len
中是8个字符,但在tty上只打印了6个字符。
预期用法:
s = 'potato\x1b[01;32mpotato\x1b[0;0mpotato'
len(s) # 32
plen(s) # 18
发布于 2013-02-15 14:47:13
至少对于ANSI TTY转义序列,这是有效的:
import re
strip_ANSI_pat = re.compile(r"""
\x1b # literal ESC
\[ # literal [
[;\d]* # zero or more digits or semicolons
[A-Za-z] # a letter
""", re.VERBOSE).sub
def strip_ANSI(s):
return strip_ANSI_pat("", s)
s = 'potato\x1b[01;32mpotato\x1b[0;0mpotato'
print s, len(s)
s1=strip_ANSI(s)
print s1, len(s1)
打印:
potato[01;32mpotato[0;0mpotato 32
potatopotatopotato 18
对于退格符\b或垂直制表符或\r vs \n --这取决于打印的方式和位置,不是吗?
https://stackoverflow.com/questions/14889454
复制相似问题