在工做中須要用到使用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: python
from termcolor import colored print colored('hello', 'red'), colored('world', 'green')
因而乎找到連接 http://pypi.python.org/pypi/termcolor 而且體驗了一下example以下: shell
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)效果以下:
記錄下 spa