python库termcolor用法

termcolor是python中标注文本颜色的库

ANSII Color formatting for output in terminal. 利用termcolor查看log,进行代码调试,清晰标出自己想要查看的部分。

帮助文档

FUNCTIONS
    colored(text, color=None, on_color=None, attrs=None)
    Colorize text.
    
    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.
    
    Example:
        colored('Hello, World!', 'red', 'on_grey', ['blue', 'blink'])
        colored('Hello, World!', 'green')
    
    cprint(text, color=None, on_color=None, attrs=None, **kwargs)
    Print colorize text.
    
    It accepts arguments of print function.

DATA
    ATTRIBUTES = {'blink': 5, 'bold': 1, 'concealed': 8, 'dark': 2, 'rever...
    COLORS = {'blue': 34, 'cyan': 36, 'green': 32, 'grey': 30, 'magenta': ...
    HIGHLIGHTS = {'on_blue': 44, 'on_cyan': 46, 'on_green': 42, 'on_grey':...
    RESET = '\x1b[0m'
    VERSION = (1, 1, 0)
    __ALL__ = ['colored', 'cprint']
    print_function = _Feature((2, 6, 0, 'alpha', 2), (3, 0, 0, 'alpha', 0)...

color:字体颜色,on_color:背景颜色

代码样例

import sys
from termcolor import colored, cprint
 
# 红色背景字符不发光
text = colored('Hello, World!', 'red', attrs=['reverse', 'blink'])
print(text)
 
# 红色背景上显示绿色字符
cprint('Hello, World!', 'green', 'on_red')
 
# 青蓝色背景上显示绿色字符
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)

1225554-20170915115540922-2076344938.png

转载于:https://www.cnblogs.com/everfight/p/python_termcolor.html

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值