Python控制台中的粗体格式

问题描述 投票:2回答:3

我在Python代码中定义了一个列表。

list = ['kumar','satheesh','rajan']    
BOLD = '\033[1m'

for i, item in enumerate(list):
   list[i] = BOLD + item

print (list)

但我得到了output as ['\x1b[1mfsdfs', '\x1b[1mfsdfsd', '\x1b[1mgdfdf']

但所需的输出是['kumar','satheesh','rajan']

如何使用format list elements bold中的python

python console ansi output-formatting
3个回答
5
投票

您还需要指定END = '\033[0m'

list = ['kumar','satheesh','rajan']

BOLD = '\033[1m'
END = '\033[0m'

for each in list:
    print('{}{}{}'.format(BOLD, each, END))

为了使列表本身大胆,如['kumar','satheesh','rajan']:

print('{}{}{}'.format(BOLD, list, END))

1
投票

尝试打印如下

for each in list:
    print(each)

以下是输出enter image description here


0
投票

Kalyan提供了一种可能的方式,但未能解释原因。

当要求Python打印列表时,它将输出列表标记的开始和结束([])以及列表中所有项目的表示。对于字符串,这意味着将转义不可打印的字符。

除了单独打印物品外,你几乎无能为力

for i lst:
    print i

或者使用join构建一个唯一的字符串:

string_to_output = '[' + ', '.join(lst) + ']'
print(string_to_output)

顺便说一句,正如Austin '\x01b[1m所解释的那样,要求ANSI兼容终端以粗体模式传递,因此您必须一次使用'\x01b[0m恢复到正常模式。

请记住:ANSI在Linux终端仿真中很常见,但它仍远未普及......只是在使用它之前尝试使用export TERM=tvi950(和google for vt100和tvi950了解更多)...

© www.soinside.com 2019 - 2024. All rights reserved.