如何以彩色打印控制台?
问题描述:
如何使用python打印以彩色打印。例如如何以彩色打印控制台?
print('This should be red')
print('This should be green')
现在一切都是黑色背景上的白色文字。如果有帮助,我使用Ubuntu。
答
这样定义颜色:
W = '\033[0m' # white (normal)
R = '\033[31m' # red
G = '\033[32m' # green
O = '\033[33m' # orange
B = '\033[34m' # blue
P = '\033[35m' # purple
print(R+"hello how are you"+W)
演示:
看到所有的颜色代码在这里:Color Codes
+1
你应该提供一个链接到颜色代码。 – 2014-12-03 07:07:03
+0
在windows上,你可能还需要'colorama'包(参见这个重复的问题)。 – 2017-12-06 22:27:08
答
使用诸如colorconsole
模块更容易:
pip install colorconsole
然后例如
from colorconsole import terminal
screen = terminal.get_terminal(conEmu=False)
screen.cprint(4, 0, "This is red\n")
screen.cprint(10, 0, "This is light green\n")
screen.cprint(0, 11, "This is black on light cyan\n")
screen.reset_colors()
它也支持256/24位颜色(如果可用)。
http://stackoverflow.com/questions/11672876/colored-console-output-in-linux – PeterMmm 2014-12-03 06:49:07
[用终端打印颜色使用Python?](http://stackoverflow.com/questions/287871)/print-in-terminal-with-colors-using-python) – Rufflewind 2014-12-03 06:59:49