也许你希望在终端上输出一些带有颜色或者粗体、下划线等样式的信息,就像man中的那样,那么这篇文章将会起到些许作用。
事件起因
在Python开发项目过程中,为了方便调试代码,经常会向stdout中输出一些日志,默认的这些日志就直接显示在了终端中。
但是很杂乱的信息显示在一起,往往没有重点,一个一个找我们需要的信息往往特别复杂.

Linux下的终端设置
linux终端颜色设置信息
在Linux终端中,使用转义序列来进行如上所述的显示,转义序列以ESC开头,即ASCII码下的\033,其格式为:
\033[显示方式;前景色;背景色m
显示方式、前景色、背景色至少一个存在即可。
格式:\033[显示方式;前景色;背景色m
说明
前景色 |
背景色 |
颜色 |
30 |
40 |
黑色 |
31 |
41 |
红色 |
32 |
42 |
绿色 |
33 |
43 |
黃色 |
34 |
44 |
蓝色 |
35 |
45 |
紫红色 |
36 |
46 |
青蓝色 |
37 |
47 |
白色 |
显示方式
显示方式 |
意义 |
0 |
终端默认设置 |
1 |
高亮显示 |
4 |
使用下划线 |
5 |
闪烁 |
7 |
反白显示 |
8 |
不可见 |
例子
\033[1;31;40m
\033[0m
Linux下解决
STYLE = {
'fore':
{
'black' : 30,
'red' : 31,
'green' : 32,
'yellow' : 33,
'blue' : 34,
'purple' : 35,
'cyan' : 36,
'white' : 37,
},
'back' :
{
'black' : 40,
'red' : 41,
'green' : 42,
'yellow' : 43,
'blue' : 44,
'purple' : 45,
'cyan' : 46,
'white' : 47,
},
'mode' :
{
'mormal' : 0,
'bold' : 1,
'underline' : 4,
'blink' : 5,
'invert' : 7,
'hide' : 8,
},
'default' :
{
'end' : 0,
},
}
def UseStyle(string, mode = '', fore = '', back = ''):
mode = '%s' % STYLE['mode'][mode] if STYLE['mode'].has_key(mode) else ''
fore = '%s' % STYLE['fore'][fore] if STYLE['fore'].has_key(fore) else ''
back = '%s' % STYLE['back'][back] if STYLE['back'].has_key(back) else ''
style = ';'.join([s for s in [mode, fore, back] if s])
style = '\033[%sm' % style if style else ''
end = '\033[%sm' % STYLE['default']['end'] if style else ''
return '%s%s%s' % (style, string, end)
def TestColor( ):
print UseStyle('正常显示')
print ''
print "测试显示模式"
print UseStyle('高亮', mode = 'bold'),
print UseStyle('下划线', mode = 'underline'),
print UseStyle('闪烁', mode = 'blink'),
print UseStyle('反白', mode = 'invert'),
print UseStyle('不可见', mode = 'hide')
print ''
print "测试前景色"
print UseStyle('黑色', fore = 'black'),
print UseStyle('红色', fore = 'red'),
print UseStyle('绿色', fore = 'green'),
print UseStyle('黄色', fore = 'yellow'),
print UseStyle('蓝色', fore = 'blue'),
print UseStyle('紫红色', fore = 'purple'),
print UseStyle('青蓝色', fore = 'cyan'),
print UseStyle('白色', fore = 'white')
print ''
print "测试背景色"
print UseStyle('黑色', back = 'black'),
print UseStyle('红色', back = 'red'),
print UseStyle('绿色', back = 'green'),
print UseStyle('黄色', back = 'yellow'),
print UseStyle('蓝色', back = 'blue'),
print UseStyle('紫红色', back = 'purple'),
print UseStyle('青蓝色', back = 'cyan'),
print UseStyle('白色', back = 'white')
print ''
if __name__ == '__main__':
TestColor( )
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132

附上用于测试的Linux下C程序
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void)
{
int left, right;
while(printf("\033[31m"),
scanf("%d%d", &left, &right) != EOF)
{
printf("\033[1;32m%d\033[0m\n", left + right);
}
return EXIT_SUCCESS;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
window下终端颜色显示
终端I/O库WConio
项目地址:http://newcenturycomputers.net/projects/wconio.html
在linux系统中,终端内可以通过curse模块或控制字符来输出彩色文本,但是在windows系统中没有curse模块也不能用控制字符,只能调用win32console模块中的控制台相关函数。直接调用这些函数还是比较麻烦的,因此有人弄了个WConio模块,封装了这些函数的功能。使用WConio,彩色文本的输出变得简单:
import WConio
attr=WConio.gettextinfo()[4] #保存默认文本颜色
WConio.textcolor(WConio.RED) #将后续输出的文本的颜色设为红色
print "红色的文字"
WConio.settextattr(attr) #回复默认的文本颜色
调用底层C库
该代码片段来自于: http://www.sharejs.com/codes/python/8665
import ctypes
STD_INPUT_HANDLE = -10
STD_OUTPUT_HANDLE= -11
STD_ERROR_HANDLE = -12
FOREGROUND_BLACK = 0x0
FOREGROUND_BLUE = 0x01
FOREGROUND_GREEN= 0x02
FOREGROUND_RED = 0x04
FOREGROUND_INTENSITY = 0x08
BACKGROUND_BLUE = 0x10
BACKGROUND_GREEN= 0x20
BACKGROUND_RED = 0x40
BACKGROUND_INTENSITY = 0x80
class Color:
''' See http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winprog/winprog/windows_api_reference.asp
for information on Windows APIs. - www.sharejs.com'''
std_out_handle = ctypes.windll.kernel32.GetStdHandle(STD_OUTPUT_HANDLE)
def set_cmd_color(self, color, handle=std_out_handle):
"""(color) -> bit
Example: set_cmd_color(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY)
"""
bool = ctypes.windll.kernel32.SetConsoleTextAttribute(handle, color)
return bool
def reset_color(self):
self.set_cmd_color(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE)
def print_red_text(self, print_text):
self.set_cmd_color(FOREGROUND_RED | FOREGROUND_INTENSITY)
print print_text
self.reset_color()
def print_green_text(self, print_text):
self.set_cmd_color(FOREGROUND_GREEN | FOREGROUND_INTENSITY)
print print_text
self.reset_color()
def print_blue_text(self, print_text):
self.set_cmd_color(FOREGROUND_BLUE | FOREGROUND_INTENSITY)
print print_text
self.reset_color()
def print_red_text_with_blue_bg(self, print_text):
self.set_cmd_color(FOREGROUND_RED | FOREGROUND_INTENSITY| BACKGROUND_BLUE | BACKGROUND_INTENSITY)
print print_text
self.reset_color()
if __name__ == "__main__":
clr = Color()
clr.print_red_text('red')
clr.print_green_text('green')
clr.print_blue_text('blue')
clr.print_red_text_with_blue_bg('background')
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61

跨平台解决方案colorama
项目地址https://pypi.python.org/pypi/colorama

示例
from colorama import init, Fore, Back, Style
if __name__ == "__main__":
init(autoreset=True)
print(Fore.RED + 'some red text')
print(Back.GREEN + 'and with a green background')
print(Style.DIM + 'and in dim text')
print('back to normal now')
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
安装
使用pip安装
pip install colorama
下载源码安装, 然后打开cmd进入源码目录
python setup.py build
python setup.py install
转载:http://blog.csdn.net/gatieme/article/details/45439671