1、采用原生转义字符序列,对Windows有的版本不支持(比如win7),完美支持Linux
实现过程:
终端的字符颜色是用转义序列控制的,是文本模式下的系统显示功能,和具体的语言无关。
转义序列是以ESC开头,即用\033来完成(ESC的ASCII码用十进制表示是27,用八进制表示就是033)。
格式:
开头部分:\033[显示方式;前景色;背景色m + 结尾部分:\033[0m
注意:开头部分的三个参数:显示方式,前景色,背景色是可选参数,可以只写其中的某一个;另外由于表示三个参数不同含义的数值都是唯一的没有重复的,所以三个参数的书写先后顺序没有固定要求,系统都能识别;但是,建议按照默认的格式规范书写。 对于结尾部分,其实也可以省略,但是为了书写规范,建议\033[***开头,\033[0m结尾。用这种原生的转义序列输出,在linux下完全支持,但是在windows下确存在兼容问题,比如在win10下可以正常显示颜色,在win7下确不支持。因此可以使用python标准库提供的colorama模块输出彩色字体,这个模块是跨平台的,内部实现也是采用转义序列来显示颜色的,只不过对windows平台做了特殊处理,因此完全兼容linux和windows各个版本。数值参数表示的意义:说明:
前景色 | 背景色 | 颜色 |
30 | 40 | 黑色 |
31 | 41 | 红色 |
32 | 42 | 绿色 |
33 | 43 | 黄色 |
34 | 44 | 蓝色 |
35 | 45 | 紫红色 |
36 | 46 | 青蓝色 |
37 | 47 | 白色 |
显示方式:
显示方式 | 意义 |
0 | 终端默认设置 |
1 | 高亮显示 |
4 | 使用下划线 |
5 | 闪烁 |
7 | 反白显示 |
8 | 不可见 |
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# @Time : 2018/4/29 10:27
# @Author : yang
# @File : Colored_Escape_character.py
# @Software: PyCharm
#--------------------------------
#显示格式:\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):
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 green!'))
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!'))
输出结果:
2、采用Python标准库colorama模块–兼容linux和windows各个版本:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# @Time : 2018/4/29 10:57
# @Author : yang
# @File : Colored_Colorama_module.py
# @Software: PyCharm
#--------------colorama模块的一些常量-------
#colorama是一个python专门用来在控制台、命令行输出彩色文字的模块,可以跨平台使用
# 在windows下linux下都工作良好,如果你想让控制台的输出信息更漂亮一些,可以使用给这个模块。
# 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 balck(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 green!'))
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!'))
termcolor是一个python包,可以改变控制台输出的颜色,支持各种terminal(WINDOWS的cmd.exe除外)。
支持下列的文字颜色:
grey, red, green, yellow, blue, magenta, cyan, white
支持下列的背景高亮:
on_grey, on_red, on_green, on_yellow, on_blue, on_magenta, on_cyan, on_white
支持下列属性:
bold, dark, underline, blink, reverse, concealed
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# @Time : 2018/4/29 16:49
# @Author : yang
# @File : Colored_Termcolor_module.py
# @Software: PyCharm
import sys
from termcolor import colored,cprint
text = colored('Hello,World!','red',attrs=['reverse','blink']) #colored(text, color=None, on_color=None, attrs=None)
# Available text colors:
# red, green, yellow, blue, magenta, cyan, white. # Available text highlights:
# on_red, on_green, on_yellow, on_blue, on_magenta, on_cyan, on_white. # Available attributes:
# bold, dark, underline, blink, reverse, concealed.
#print('\033[5;7;31mHello,World!\033[0m') print(text) cprint('Hello,World!','green','on_red')
#cprint('Hello,World!','green','on_red',attrs=['bold'])
#def cprint(text, color=None, on_color=None, attrs=None, **kwargs) print_red_on_cyan = lambda x:cprint(x,'red','on_cyan')
print_red_on_cyan('Hello,World!')
print_red_on_cyan('Hello,Universe!')
for i in range(10):
cprint(i,'magenta',end=' ')
cprint('Attention!','red',attrs=['bold'],file = sys.stderr)
输出结果:
参考:
1、https://pypi.org/project/colorama/
2、https://pypi.org/project/termcolor/#description
3、https://www.cnblogs.com/hellojesson/p/5961570.html
4、https://stackoverflow.com/questions/287871/print-in-terminal-with-colors/3332860#3332860