您的位置:首页 > 编程语言 > Python开发

python编码问题1

2015-12-16 00:00 483 查看
在windows的cmd控制台的打印(编码GBK):

>>> print repr('哈')
'\xb9\xfe'
>>> ('\xb9\xfe').decode('gbk')
u'\u54c8'
>>> ('\xb9\xfe').decode('gb2312')
u'\u54c8'
>>> ('\xb9\xfe').decode('gbk').encode('utf8')
'\xe5\x93\x88'
>>> ('\xb9\xfe').decode('gb2312').encode('utf8')
'\xe5\x93\x88'

>>> '\xb9\xfe'=='哈'
True
>>> '\xe5\x93\x88'=='哈'
False

#查看本机编码方式
>>> print sys.getdefaultencoding()
ascii
>>> print sys.getfilesystemencoding()
mbcs

#python文档对mbcs的解释:Windows only: Encode operand according to the ANSI codepage (CP_ACP)

#报错
>>> print ('哈').decode('utf8')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python27\Lib\encodings\utf_8.py", line 16, in decode
return codecs.utf_8_decode(input, errors, True)
UnicodeDecodeError: 'utf8' codec can't decode byte 0xb9 in position 0: invalid s
tart byte

在设置了默认编码为UTF8的pycharm的控制台:

In[2]: print repr("哈")
'\xe5\x93\x88'
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: