无法在终端上进行彩色打印 [英] Not able to print in color on terminal

查看:68
本文介绍了无法在终端上进行彩色打印的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在终端中使用Python打印颜色时给出的答案?,我尝试使用以下代码在控制台/终端上以彩色打印:

  RED = \e [ 31m 
NORMAL = \e [0m

print( TESTING)
print(RED + TESTING + NORMAL)
print( TESTING )

但是,它不起作用,仅给出以下输出:

 正在测试
[e [31mTESTING\e [0m#黑色,表示这里的颜色。
测试

问题出在哪里,如何解决?我在Debian Stable Linux上使用Python版本3.5.3。

解决方案

您发现错误的答案; \e 在Python中不是有效的转义序列。在某些 echo 实现中, \e ASCII ESC字符,但是在Python中,您需要使用其他符号。



该页面上的其余答案使用正确的格式,即 \x1b \033 。 / p>

As given in an answer on Print in terminal with colors using Python? , I am trying to print in color on console/terminal using following code:

RED = "\e[31m"
NORMAL = "\e[0m"

print("TESTING")
print(RED+"TESTING"+NORMAL)
print("TESTING")

However, it is not working and only giving following output:

TESTING
\e[31mTESTING\e[0m      # IN BLACK, THOUGH IT IS SHOWING COLOR HERE.
TESTING

Where is the problem and how can it be solved? I am using Python version 3.5.3 on Debian Stable Linux.

解决方案

You have found a wrong answer; \e is not a valid escape sequence in Python. In some echo implementations, \e is an escape sequence for the ASCII ESC character, but in Python you need to use a different notation.

The rest of the answers on that page use correct forms, either \x1b or \033.

这篇关于无法在终端上进行彩色打印的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆