【问题标题】:Why printing in color in a Windows terminal in Python does not work? [duplicate]为什么在 Python 的 Windows 终端中进行彩色打印不起作用? [复制]
【发布时间】:2018-07-04 20:44:09
【问题描述】:

我可以在 ipython 中将 test1 打印为绿色,将 test2 打印为红色:

print ('\033[92m' + "test1")
print ('\033[91m' + "test2")

但在终端中以白色打印给我以下错误:

[92mtest1
[91mtest2

【问题讨论】:

  • pip install pygments?
  • 感谢它与 colorama 一起使用

标签: python windows python-3.x terminal


【解决方案1】:

Windows 终端不像其他(vt100 兼容)终端那样处理用于着色文本的 ansi 编码。如果您希望能够在 Windows 中为输出着色,最好的选择可能是 coloramaAnsicon 或其他一些外部程序来处理输出到 Windows 终端的着色。

【讨论】:

  • 感谢它与 Colorama 一起使用
  • 除了 Windows 10 控制台不支持虚拟终端序列。通过在注册表项“HKCU\Console”中设置值为 1 的“VirtualTerminalLevel”REG_DWORD,可以默认为所有控制台窗口启用它。
猜你喜欢
  • 2018-04-17
  • 1970-01-01
  • 2021-03-01
  • 2019-04-29
  • 1970-01-01
  • 2019-06-26
  • 1970-01-01
  • 1970-01-01
  • 2013-02-28
相关资源
最近更新 更多