python termcolor 模块介绍

在工作中需要用到使用python输出颜色字符,之前一致间接调用的shell的彩色打印,其实python有更好的彩色打印模块 termcolor, 在http://stackoverflow.com/questions/287871/print-in-terminal-with-colors-using-python中发现这样一句:I'm surprised no one has mentioned the Python termcolor module. Usage is pretty simple:

from termcolor import colored
print colored('hello', 'red'), colored('world', 'green')

于是乎找到链接 http://pypi.python.org/pypi/termcolor 并且体验了一下example如下:

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)
效果如下:

记录下

转载于:https://my.oschina.net/sanpeterguo/blog/88100

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值