Python colorama
项目地址:https://pypi.python.org/pypi/colorama
使用背景
simply by manually printing ANSI sequences from your own code:
print('\033[31m' + 'some red text')
print('\033[30m') # and reset to default color
这种方式简直low爆了。
Colorama带你飞
from colorama import init
init()
Colored Output
from colorama import Fore, Back, Style
print(Fore.RED + 'some red text')
print(Back.GREEN + 'and with a green background')
print(Style.GRIGHT + 'and in dim text')
print(Style.RESET_ALL)
print('back to normal now')
Available formatting constants are:
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
注意:To stop using colorama before your program exits, simply call deinit(). This will restore stdout and stderr to their original values, so that Colorama is disabled. To resume using Colorama again, call reinit(); it is cheaper to calling init() again (but does the same thing).
然而:If you find yourself repeatedly sending reset sequences to turn off color changes at the end of every print, then init(autoreset=True) will automate that
from colorama import init
init(autoreset=True)
print(Fore.RED + 'some red text')
print('automatically back to default color again')