首页 > 解决方案 > ANSI escape code wont work on python interpreter

问题描述

ANSI code wont work on my python interpreter

I wanted to color some of my prints on the project. I looked up on how to color printed characters and found the ANSI escape codes, so i tried it up on the interpreter, but it wont work.

for example:

print("\033[32m Hello")

it Gives me <-[32m Hello (an arrow left sign).

how do i make it work? can it work on python's interpreter? if not, where should i use it?

标签: pythonansi-escape

解决方案


您最好安装其他软件包来帮助生成 ANSI 序列,iirc win32 控制台本身不支持ANSI 颜色。一种选择是安装colorama。以下代码段打印出红色文本。

import colorama
from colorama import Fore, Back, Style
colorama.init()
print(Fore.RED + 'This is red')

编辑:经过更多研究,我意识到 Windows 10支持ANSI Escape Sequence,您可以通过调用来实现。如果您打算复制和粘贴,请使用此选项。

import os
os.system("echo [31mThis is red[0m")

不过我还是更喜欢前者。


推荐阅读