Python2.7打印中文乱码处理

Posted 亦游

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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("测试一下")

以上是关于Python2.7打印中文乱码处理的主要内容,如果未能解决你的问题,请参考以下文章