Python终端输出彩色字符方法详解
有时候需要在终端显示彩色的字符,即根据需要显示不同颜色的字符串,比如我们要在终端打印一行错误提示信息,要把它弄成红色的。其实这个在Python中很好实现,使用转义序列来实现不同颜色的显示,转义序列以ESC开头,它的ASCII码八进制为\033。显示格式为:\033[显示方式;前景色;背景色m
用这种原生的转义序列输出,在linux下完全支持,但是在windows下确存在兼容问题,比如在win10下可以正常显示颜色,在win7下确不支持。因此可以使用python标准库提供的colorama模块
输出彩色字体,这个模块是跨平台的,内部实现也是采用转义序列来显示颜色的,只不过对windows平台做了特殊处理,因此完全兼容linux和windows各个版本。
以下封装了一个Colored类,提供了两个版本,第一个版本采用原生的转义字符序列输出各种颜。
第二个版本用python标准库的colorama模块兼容windows和linux。当要在终端打印彩色字体时直接调用对应的方法即可,很方便。
一.Colored版本
1:采用原生的转义字符序列---对windows有的版本不支持(比如win7),linux完美支持
#coding:gbk
#------------------------------------------------
#python终端显示彩色字符类,可以调用不同的方法
#选择不同的颜色.使用方法看示例代码就很容易明白.
#------------------------------------------------
#
#显示格式:\033[显示方式;前景色;背景色m
#------------------------------------------------
#显示方式说明
#0终端默认设置
#1高亮显示
#4使用下划线
#5闪烁
#7反白显示
#8不可见
#22非粗体
#24非下划线
#25非闪烁
#
#前景色背景色颜色
#3040黑色
#3141红色
#3242绿色
#3343黃色
#3444蓝色
#3545紫红色
#3646青蓝色
#3747白色
#------------------------------------------------
classColored(object):
#显示格式:\033[显示方式;前景色;背景色m
#只写一个字段表示前景色,背景色默认
RED='\033[31m'#红色
GREEN='\033[32m'#绿色
YELLOW='\033[33m'#黄色
BLUE='\033[34m'#蓝色
FUCHSIA='\033[35m'#紫红色
CYAN='\033[36m'#青蓝色
WHITE='\033[37m'#白色
#:nocolor
RESET='\033[0m'#终端默认颜色
defcolor_str(self,color,s):
return'{}{}{}'.format(
getattr(self,color),
s,
self.RESET
)
defred(self,s):
returnself.color_str('RED',s)
defgreen(self,s):
returnself.color_str('GREEN',s)
defyellow(self,s):
returnself.color_str('YELLOW',s)
defblue(self,s):
returnself.color_str('BLUE',s)
deffuchsia(self,s):
returnself.color_str('FUCHSIA',s)
defcyan(self,s):
returnself.color_str('CYAN',s)
defwhite(self,s):
returnself.color_str('WHITE',s)
#----------使用示例如下:-------------
color=Colored()
printcolor.red('Iamred!')
printcolor.green('Iamgree!')
printcolor.yellow('Iamyellow!')
printcolor.blue('Iamblue!')
printcolor.fuchsia('Iamfuchsia!')
printcolor.cyan('Iamcyan!')
printcolor.white('Iamwhite')
颜色对比图(根据需要自己设置对应的值):
运行效果:
二.Colored版本
2:采用python标准库的colorama模块--兼容linux和windows各个版本:
#-----------------colorama模块的一些常量---------------------------
#Fore:BLACK,RED,GREEN,YELLOW,BLUE,MAGENTA,CYAN,WHITE,RESET.
#Back:BLACK,RED,GREEN,YELLOW,BLUE,MAGENTA,CYAN,WHITE,RESET.
#Style:DIM,NORMAL,BRIGHT,RESET_ALL
#
fromcoloramaimportinit,Fore,Back,Style
init(autoreset=True)
classColored(object):
#前景色:红色背景色:默认
defred(self,s):
returnFore.RED+s+Fore.RESET
#前景色:绿色背景色:默认
defgreen(self,s):
returnFore.GREEN+s+Fore.RESET
#前景色:黄色背景色:默认
defyellow(self,s):
returnFore.YELLOW+s+Fore.RESET
#前景色:蓝色背景色:默认
defblue(self,s):
returnFore.BLUE+s+Fore.RESET
#前景色:洋红色背景色:默认
defmagenta(self,s):
returnFore.MAGENTA+s+Fore.RESET
#前景色:青色背景色:默认
defcyan(self,s):
returnFore.CYAN+s+Fore.RESET
#前景色:白色背景色:默认
defwhite(self,s):
returnFore.WHITE+s+Fore.RESET
#前景色:黑色背景色:默认
defblack(self,s):
returnFore.BLACK
#前景色:白色背景色:绿色
defwhite_green(self,s):
returnFore.WHITE+Back.GREEN+s+Fore.RESET+Back.RESET
color=Colored()
printcolor.red('Iamred!')
printcolor.green('Iamgree!')
printcolor.yellow('Iamyellow!')
printcolor.blue('Iamblue!')
printcolor.magenta('Iammagenta!')
printcolor.cyan('Iamcyan!')
printcolor.white('Iamwhite!')
printcolor.white_green('Iamwhitegreen!')
运行效果:
更多关于Python终端输出彩色字符方法请查看下面的相关链接
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。