首页 > 解决方案 > Termcolor“彩色”输出奇怪的字符

问题描述

我已经将python安装到我的命令提示符(cmd)顺便说一句

命令提示符:

C:\Users\*****> python -m pip install termcolor

Collecting termcolor
  Downloading https://files.pythonhosted.org/packages/8a/48/a76be51647d0eb9f10e2a4511bf3ffb8cc1e6b14e9e4fab46173aa79f981/termcolor-1.1.0.tar.gz
Installing collected packages: termcolor
  Running setup.py install for termcolor ... done
Successfully installed termcolor-1.1.0
You are using pip version 9.0.1, however version 19.3.1 is available.
You should consider upgrading via the 'python -m pip install --upgrade pip' command.

C:\Users\*****> python
#going into python now

Python 3.6.3 (v3.6.3:2c5fed8, Oct  3 2017, 17:26:49) [MSC v.1900 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from termcolor import colored
>>> print(colored("hi","green"))

这里是奇怪的输出: [32mhi[0m 我试过 python shell 但它不起作用。(见下图) Python Shell,不起作用

标签: pythoncmdtermcolor

解决方案


如果您是 Windows 用户,则需要多做一步。安装名为 colorama 的模块。然后,在代码的顶部包含以下代码:

from colorama import init
init(autoreset = True)

推荐阅读