【问题标题】:Why does termcolor not work in python27 windows?为什么 termcolor 在 python27 窗口中不起作用?
【发布时间】:2018-09-30 00:23:00
【问题描述】:

我刚刚在 windows8.1 上为 python 2.7 安装了 termcolor。当我尝试打印彩色文本时,我得到了奇怪的输出。

from termcolor import colored
print colored('Hello world','red')

结果如下:

[31mHello world[0m

帮助摆脱这个问题。谢谢,提前

【问题讨论】:

标签: python-2.7 termcolor


【解决方案1】:

termcolorcolored 在 python 2.7 下工作得非常好,我无法在我的 Mac/Linux 上复制你的错误。

如果你查看colored的源代码,它基本上会以格式打印字符串

\033[%dm%s\033[0m' % (COLORS[color], text)

不知何故,您的终端环境无法识别 unix/linux 系统中用于设置 xterm 前景色的非打印转义序列。

【讨论】:

    【解决方案2】:

    请参阅此stackOverflow 帖子。

    它基本上说,为了让转义序列在 Windows 中工作,你需要先运行 os.system('color')。

    例如:

    import termcolor
    import os
    os.system('color')
    print(termcolor.colored("Stack Overflow", "green")
    

    【讨论】:

      猜你喜欢
      • 2020-09-10
      • 2015-09-27
      • 2016-02-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-08
      • 1970-01-01
      相关资源
      最近更新 更多