print 中文输出乱码

来源:互联网 发布:如何在手机上编程 编辑:程序博客网 时间:2024/05/02 05:18
#!/usr/bin/python
# -*- coding: utf-8 -*-

print "hello world  我是中文"


存储为hello.py文件 在cmd运行, 中文为乱码


资料查找:

  源码文件的编码格式为utf-8,但是window的本地默认编码是gbk,所以在控制台直接打印utf-8的字符串当然是乱码了!

解决方法1:

mystr ="hello world 我是中文"
print mystr.decode('utf-8').encode('gbk')

解决方法2:

mystr ="hello world 我是中文"
import sys
ype = sys.getfilesystemencoding()

print mystr.decode('utf-8').encode(type)

原创粉丝点击