如何在Python终端中打印彩色文本? [英] How to print colorful text in Python terminal?
问题描述
我知道在Stackoverflow 此处提出了这个问题
I know this question have been raised at Stackoverflow here
因此,我尝试从 joil
的答案中尝试以下代码。
So I've tried the following code from joeld
's answer.
下面是IDLE中的代码(我正在使用Python 2.7):
Here is the code in IDLE (I am using Python 2.7):
print '\033[95m'+'my text'+'\033[95m'
,我得到的输出没有颜色已更改:
and I got the output without color changed:
[95mmy text[95m
======================================== ======================================
===============================================================================
然后我也尝试了软件包 colorama
。软件包安装在 cmd
内:
Then I also tried the package colorama
. The package was installed within cmd
:
python setup.py install
并在Aptana Studio 3中尝试了以下代码:
and tried following code in Aptana Studio 3:
from colorama import *
print (Fore.GREEN + 'Green text')
print (Fore.Red + 'Red text')
我得到以下输出:
[32mGreen text
Traceback (most recent call last):
File "C:\Users\My Documents\Aptana Studio 3 Workspace\Practice\test_colorama.py", line 12, in <module>
print (Fore.Red + 'Red text')
AttributeError: 'AnsiCodes' object has no attribute 'Red'
========================================= =====================================
===============================================================================
所以现在我真的很困惑。这些解决方案已被投票数百次,这表明它应该有效,但就我而言似乎无效。
So now I am really confused. These solutions are upvoted hundreds of times which implies that it should be effective, but in my case it seems not.
我可以在终端机中打印彩色文本吗?还是控制台?
May I know how I can print colorful text in the terminal or console?
非常感谢。
推荐答案
没有红色
>>> import colorama
>>> colorama.Fore.Red
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'AnsiCodes' object has no attribute 'Red'
但是, RED
存在:
>>> colorama.Fore.RED
'\x1b[31m'
这篇关于如何在Python终端中打印彩色文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!