问题
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?
回答1:
You are best off installing other packages to help generate the ANSI sequences, iirc win32 console does not support ANSI colours natively. One option is to install colorama. The following snippet prints out red text.
import colorama
from colorama import Fore, Back, Style
colorama.init()
print(Fore.RED + 'This is red')
Edit: Upon researching a little more, I've realised that Windows 10 has support for ANSI Escape Sequence and you can do so by calling. Use this if you intend on copy and pasting.
import os
os.system("echo [31mThis is red[0m")
However i'd still prefer the former.
来源:https://stackoverflow.com/questions/57733862/ansi-escape-code-wont-work-on-python-interpreter