Python2.7打印中文乱码处理

总是有些编码的问题,光这样是无效的:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

得做如下处理:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys

sys_encoding = sys.getfilesystemencoding()
def printcn(msg):
    print(msg.decode('utf-8').encode(sys_encoding))


if __name__ == "__main__":
    #兼容中文字符打印
    reload(sys)
    sys.setdefaultencoding("utf-8")

    printcn("测试一下")

猜你喜欢

转载自blog.csdn.net/tp7309/article/details/78534562