ANSI escape code wont work on python interpreter

拥有回忆 提交于 2021-01-29 19:39:21

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!