encode/decode in python2

>>> text = u'闷声发大财'
>>> print type(text), text
<type 'unicode'> ÃÆÉù·¢´ó²Æ
>>> print text.encode('utf8')
脙脝脡霉路垄麓贸虏脝
>>> print text.encode('utf16')
ÿþÃ
>>> print text.encode('gbk')

Traceback (most recent call last):
  File "<pyshell#28>", line 1, in <module>
    print text.encode('gbk')
UnicodeEncodeError: 'gbk' codec can't encode character u'\xc3' in position 0: illegal multibyte sequence
>>> print text.encode('gb2312')

Traceback (most recent call last):
  File "<pyshell#29>", line 1, in <module>
    print text.encode('gb2312')
UnicodeEncodeError: 'gb2312' codec can't encode character u'\xc3' in position 0: illegal multibyte sequence
>>> print text.encode('cp1252')
闷声发大财

cp1252 is the default encoding for English installations of MS windows.

 

你可能感兴趣的:(encode/decode in python2)