How can I output color printing information to the console? How can I customize my output format? The following will tell you ......
Import OS
"""
0 All attributes off default value
1 Bold (or Bright) Bold or highlighted
4 Underline
5 Blink flashing
7. Reverse display
30 Black text
31 Red text
32 Green text
33 Yellow text
34 Blue text
35 Purple text
36 Cyan text
37 White text
40 Black background
41 Red background
42 Green background
43 Yellow background
44 Blue background
45 Purple background
46 Cyan background
47 White background
"""
Def colored (text, color = None, on_color = None, attrs = None ):
Fmt_str = '\ x1B [; % dm % s \ x1B [0m'
If color is not None:
Text = fmt_str % (color, text)
If on_color is not None:
Text = fmt_str % (on_color, text)
If attrs is not None:
For attr in attrs:
Text = fmt_str % (color, text)
Return text
Def printError (msg ):
Print colored (msg, color = 31)
Def printWarning (msg ):
Print colored (msg, color = 33)
Def printInfo (msg ):
Print colored (msg, color = 37)
If _ name _ = '_ main __':
PrintError ("this is an error message! ")
PrintWarning ("this is a warning message! ")
PrintInfo ("this ia a info message! ")
Output:
Refer:
Http://www.oschina.net/code/snippet_87675_8599
Http://pypi.python.org/pypi/termcolor/0.1.1#downloads