使用python在终端输出带有样式的文本 发表于 2015-05-14 | 分类于 python termos colorful output | 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071#!/usr/bin/env python# -*- coding:utf-8 -*-'''Author : mythDate : 15-5-14Email : belongmyth at 163.com'''Styles = dict( list(zip(list(range(1, 9)),[ 'bold', 'dark', '', 'underline', 'blink', '', 'reverse', 'concealed' ] )) )for k,v in Styles.items(): if v == '': del Styles[k]ForegroundColors = dict( list(zip(list(range(30, 38)),[ 'grey', 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'white', ] )) )BackgroundColors = dict( list(zip(list(range(40, 48)),[ 'on_grey', 'on_red', 'on_green', 'on_yellow', 'on_blue', 'on_magenta', 'on_cyan', 'on_white' ] )) )max_length = max([len(v) for v in Styles.values()]) + max([len(v) for v in ForegroundColors.values()]) + max([len(v) for v in BackgroundColors.values()])def print_format_table(): """ prints table of formatted text format options """ for style, styleMsg in Styles.items(): for fg, fgMsg in ForegroundColors.items(): s1 = '' for bg, bgMsg in BackgroundColors.items(): format = ';'.join([str(style), str(fg), str(bg)]) formatMsg = '-'.join([styleMsg, fgMsg, bgMsg]).center(max_length+3) s1 += '\x1b[%sm %s \x1b[0m' % (format, formatMsg) print s1 print ''print_format_table()