有时候需要在终端显示彩色的字符,即根据需要显示不同颜色的字符串,比如我们要在终端打印一行错误提示信息,要把它弄成红色的。其实这个在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 非闪烁 # # 前景色 背景色 颜色 # 30 40 黑色 # 31 41 红色 # 32 42 绿色 # 33 43 黃色 # 34 44 蓝色 # 35 45 紫红色 # 36 46 青蓝色 # 37 47 白色 # ------------------------------------------------ class Colored(object): # 显示格式: \033[显示方式;前景色;背景色m # 只写一个字段表示前景色,背景色默认 RED = '\033[31m' # 红色 GREEN = '\033[32m' # 绿色 YELLOW = '\033[33m' # 黄色 BLUE = '\033[34m' # 蓝色 FUCHSIA = '\033[35m' # 紫红色 CYAN = '\033[36m' # 青蓝色 WHITE = '\033[37m' # 白色 #: no color RESET = '\033[0m' # 终端默认颜色 def color_str(self, color, s): return '{}{}{}'.format( getattr(self, color), s, self.RESET ) def red(self, s): return self.color_str('RED', s) def green(self, s): return self.color_str('GREEN', s) def yellow(self, s): return self.color_str('YELLOW', s) def blue(self, s): return self.color_str('BLUE', s) def fuchsia(self, s): return self.color_str('FUCHSIA', s) def cyan(self, s): return self.color_str('CYAN', s) def white(self, s): return self.color_str('WHITE', s) # ----------使用示例如下:------------- color = Colored() print color.red('I am red!') print color.green('I am gree!') print color.yellow('I am yellow!') print color.blue('I am blue!') print color.fuchsia('I am fuchsia!') print color.cyan('I am cyan!') print color.white('I am white')
颜色对比图(根据需要自己设置对应的值):
运行效果:
二.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 # from colorama import init, Fore, Back, Style init(autoreset=True) class Colored(object): # 前景色:红色 背景色:默认 def red(self, s): return Fore.RED + s + Fore.RESET # 前景色:绿色 背景色:默认 def green(self, s): return Fore.GREEN + s + Fore.RESET # 前景色:黄色 背景色:默认 def yellow(self, s): return Fore.YELLOW + s + Fore.RESET # 前景色:蓝色 背景色:默认 def blue(self, s): return Fore.BLUE + s + Fore.RESET # 前景色:洋红色 背景色:默认 def magenta(self, s): return Fore.MAGENTA + s + Fore.RESET # 前景色:青色 背景色:默认 def cyan(self, s): return Fore.CYAN + s + Fore.RESET # 前景色:白色 背景色:默认 def white(self, s): return Fore.WHITE + s + Fore.RESET # 前景色:黑色 背景色:默认 def black(self, s): return Fore.BLACK # 前景色:白色 背景色:绿色 def white_green(self, s): return Fore.WHITE + Back.GREEN + s + Fore.RESET + Back.RESET color = Colored() print color.red('I am red!') print color.green('I am gree!') print color.yellow('I am yellow!') print color.blue('I am blue!') print color.magenta('I am magenta!') print color.cyan('I am cyan!') print color.white('I am white!') print color.white_green('I am white green!')
运行效果:
更多关于Python终端输出彩色字符方法请查看下面的相关链接
华山资源网 Design By www.eoogi.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
华山资源网 Design By www.eoogi.com
暂无评论...
稳了!魔兽国服回归的3条重磅消息!官宣时间再确认!
昨天有一位朋友在大神群里分享,自己亚服账号被封号之后居然弹出了国服的封号信息对话框。
这里面让他访问的是一个国服的战网网址,com.cn和后面的zh都非常明白地表明这就是国服战网。
而他在复制这个网址并且进行登录之后,确实是网易的网址,也就是我们熟悉的停服之后国服发布的暴雪游戏产品运营到期开放退款的说明。这是一件比较奇怪的事情,因为以前都没有出现这样的情况,现在突然提示跳转到国服战网的网址,是不是说明了简体中文客户端已经开始进行更新了呢?
更新日志
2024年09月24日
2024年09月24日
- 罗文.2022-大地留着的记号·纪念罗文辞别廿载3CD【环球】【WAV+CUE】
- 670《脆弱敏感小女生》[FLAC/分轨][78.47MB]
- 时代少年团《「叁重楼」演唱会 -「楼间楼」重庆站LIVE》[320K/MP3][278.08MB]
- 时代少年团《「叁重楼」演唱会 -「楼间楼」重庆站LIVE》[FLAC/分轨][820.86MB]
- 许廷铿2017《THEULTIMATECOLLECTION》2CD[星梦娱乐][WAV+CUE]
- 许佳慧-独家情话[WAV+CUE]
- 张惠妹《ThePowerOfA-Mei》2018[SACD/ISO]
- 陈珊妮.2013-低调人生2CD【和气音乐】【WAV+CUE】
- 群星.2000-超级BAND.BAND.BAND【滚石】【WAV+CUE】
- 许志安.2002-On.Show演唱会2CD(2024环球红馆40复刻系列)【环球】【WAV+CUE】
- 蔡琴-哈利路亚2016[SACD/ISO]
- 陈随意2011-今生只为你[豪记][WAV+CUE]
- 陈随意2008-爱的总铺师[美华][WAV+CUE]
- 裘德《一棵树所创造的》[320K/MP3][56.21MB]
- 裘德《一棵树所创造的》[24bit 48kHz][FLAC/分轨][355.1MB]