gpt4 book ai didi

python - 为什么 termcolor 在 Windows 控制台中输出的是控制字符而不是彩色文本?

转载 作者:太空狗 更新时间:2023-10-29 17:13:21 25 4
gpt4 key购买 nike

我刚刚安装了 termcolor适用于 Windows 上的 Python 2.7。当我尝试打印彩色文本时,我得到的是颜色代码。

from termcolor import colored
print colored('Text text text', 'red')

结果如下:

Screenshot of the Windows console window with the line: "←31mText text text←[0m"

我在 Far Manager 上和尝试将脚本作为独立应用程序运行时获得了相同的结果。

最佳答案

要使 termcolor 中使用的 ANSI 颜色与 Windows 终端一起使用,您还需要导入/初始化 colorama ;

>>> from termcolor import *
>>> cprint('hello', 'red')
←[31mhello←[0m
>>> import colorama
>>> colorama.init()
>>> cprint('hello', 'red')
hello <-- in red color
>>>

关于python - 为什么 termcolor 在 Windows 控制台中输出的是控制字符而不是彩色文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21858567/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com