python格式采用织物颜色字符串的实际长度
我使用PythonFabric为终端输出着色,并使用字符串格式来对齐文本 颜色将不可见的代码添加到字符串中,如何不中断格式输出python格式采用织物颜色字符串的实际长度,python,colors,terminal,fabric,Python,Colors,Terminal,Fabric,我使用PythonFabric为终端输出着色,并使用字符串格式来对齐文本 颜色将不可见的代码添加到字符串中,如何不中断格式输出 >>> from fabric.colors import red >>> print '{:->27}'.format('line one') -------------------line one >>> print '{:->27}'.format('longer line two') ------
>>> from fabric.colors import red
>>> print '{:->27}'.format('line one')
-------------------line one
>>> print '{:->27}'.format('longer line two')
------------longer line two
>>> print '{:->27}'.format(red('line three'))
--------line three
>>>
正如汉斯所说,我们只需要加上+9
>>>
>>> print '{:->27}'.format('line one')
-------------------line one
>>> print '{:->27}'.format('longer line two')
------------longer line two
>>> print '{:->36}'.format(red('line three'))
-----------------line three
>>> print '{:->36}'.format(red('and more words'))
-------------and more words
>>> print '{:->36}'.format(red('and more words plus one'))
----and more words plus one
>>>
由于织物颜色的定义包含以下内容:
def _wrap_with(code):
def inner(text, bold=False):
c = code
if bold:
c = "1;%s" % c
return "\033[%sm%s\033[0m" % (c, text)
return inner
red = _wrap_with('31')
您的“第三行”输出的行比其他行短9个字符,
我猜红色(字符串)的输出总是包含9个不可见字符,由字符串“\033[%sm%s\033[0m”存储
因此,要修正格式,当使用红色(字符串)时,您应该键入
print '{:->(27+9)}'.format(red('line three'))
而不是
print '{:->27}'.format(red('line three'))