Python终端显示彩色字符

输出带颜色的字符串,用来显示要突出的部分。经测验,在pycharm中可行,在windows命令行中不可行。原因未知。

方法:

格式:"\033[显示方式;前景色;背景色m 需要变颜色的字符串 \033[显示方式m"
例子:"\033[1;31;47m 需要变颜色的字符串 \033[0m"
<1-高亮显示 31前景色红色 47背景色黑色--需要变颜色的字符串--0-取消颜色设置>

说明:
前景色            背景色           颜色
---------------------------------------
 30                 40             黑色
 31                 41             红色
 32                 42             绿色
 33                 43             黃色
 34                 44             蓝色
 35                 45             紫红色
 36                 46             青蓝色
 37                 47             白色
显示方式            意义
-------------------------
 0                终端默认设置(即取消颜色设置)
 1                高亮显示
 4                使用下划线
 5                闪烁
 7                反白显示
 8                不可见

测试:

print("\033[1;31;40m需要变颜色的字符串\033[0m")

结果:
这里写图片描述

另外这里还提供了两种封装(Colored类)的方法

以下封装了一个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!')

运行效果:
这里写图片描述

颜色对比图(根据需要自己设置对应的值):
这里写图片描述

参考文献:
1、https://blog.csdn.net/qianghaohao/article/details/52117082 2018.5.20
2、https://blog.csdn.net/xiaoxinyu316/article/details/39434425 2018.5.20
3、https://www.cnblogs.com/lucas0625/p/7642616.html 2018.5.20

猜你喜欢

转载自blog.csdn.net/johinieli/article/details/80388838
今日推荐