为什么 termcolor 在 Windows 控制台中输出控制字符而不是彩色文本? [英] Why does termcolor output control characters instead of colored text in the Windows console?

查看:45
本文介绍了为什么 termcolor 在 Windows 控制台中输出控制字符而不是彩色文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚在 Windows 上为 Python 2.7 安装了 ;

<预><代码>>>>从 termcolor 导入 *>>>cprint('你好', '红色')←[31mhello←[0m>>>进口彩绘>>>colorama.init()>>>cprint('你好', '红色')你好 <-- 红色>>>

I just installed termcolor for Python 2.7 on Windows. When I try to print colored text, I get the color codes instead.

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

Here is the result:

I obtain the same results on Far Manager and when I tried to run the script as a standalone application.

解决方案

To make the ANSI colors used in termcolor work with the windows terminal, you'll need to also import/init colorama;

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

这篇关于为什么 termcolor 在 Windows 控制台中输出控制字符而不是彩色文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆