Python打印的一个例子

用python肯定是为了跨平台的便利,用python时在不同平台打印需要注意的地方。

Windows和mac的终端编码不一样,尤其是在打印中文的时候

我采用的方法是在windows下将编码转换为gbk

打印时候最好能加上颜色

使用colorama这个库来实现

对Logging的封装

Logging.py
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
# coding=utf-8
# DannyHe
import os
import sys
import platform
import time
from colorama import *
init(autoreset=True)
class Logging:
DEBUG_MODE = False
@staticmethod
def _print(s, color=None):
if sys.platform != 'win32':
print color + s
else:
print color + s.decode('utf-8').encode("GBK")
@staticmethod
def debug(s):
if Logging.DEBUG_MODE:
localtime = time.strftime(
"%a %H:%M:%S", time.localtime(time.time()))
Logging._print("[DEBUG]%s %s " % (localtime, s), Fore.RESET)
@staticmethod
def info(s):
localtime = time.strftime("%a %H:%M:%S", time.localtime(time.time()))
Logging._print("[INFO]%s %s " % (localtime, s), Fore.CYAN)
@staticmethod
def warning(s):
localtime = time.strftime("%a %H:%M:%S", time.localtime(time.time()))
Logging._print("[WARNING]%s %s " % (localtime, s), Fore.YELLOW)
@staticmethod
def error(s):
localtime = time.strftime("%a %H:%M:%S", time.localtime(time.time()))
Logging._print("[ERROR]%s %s " % (localtime, s), Fore.RED)

使用

test.py
1
2
3
4
5
6
7
8
# coding=utf-8
# DannyHe
from Logging import *
Logging.DEBUG_MODE = True
Logging.debug("hello world")
Logging.info("你好")