【问题标题】:Is it the bug for cmd terminal to print colored text by python?cmd终端通过python打印彩色文本是否是错误?
【发布时间】:2019-09-16 07:07:52
【问题描述】:

我的微软cmd版本是10.0.16299.1331,python是Python 3.7.0。

对于下面的代码,颜色没有正确改变,你可以看到结果有一些奇怪的字符,比如[。

from colorama import init, Fore, Back, Style
print(Fore.RED, "hello world", Fore.RESET)

结果:

[31m hello world [39m

但是,如果我先跑到下面,似乎一切都会好起来的。

os.system('color 4') #这里4可以是1~8

有关于这个奇怪问题的 cmet 吗?是bug吗?

【问题讨论】:

  • 请看这个answer 看看它是否能解决问题
  • 感谢@hansolo,我现在工作。似乎我只在代码中错过了“init(convert=True)”...

标签: python


【解决方案1】:

在代码的页脚处使用 init()

【讨论】:

  • 为什么 init 会改变输出,尤其是在 print 之后调用?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-09-22
  • 1970-01-01
  • 1970-01-01
  • 2017-07-17
  • 1970-01-01
  • 1970-01-01
  • 2021-07-18
相关资源
最近更新 更多