Python编/解码

>>> s='我wo'
>>> s
'\xe6\x88\x91wo'
>>> u=s.decode('utf-8')
>>> u
u'\u6211wo'
>>> print u
我wo
>>> print s
我wo
>>> u=u'我wo'
>>> u
u'\u6211wo'
>>> s=u.encode('utf-8')
>>> s
'\xe6\x88\x91wo'
>>> s='h\xee'
>>> type(s)

>>> print s
h
>>> u=u'h\xee'
>>> type(u)

>>> print u
hî

>>> s='我wo'
>>> s.unicode()
Traceback (most recent call last):
  File "", line 1, in 
AttributeError: 'str' object has no attribute 'unicode'
>>> unicode(s)
Traceback (most recent call last):
  File "", line 1, in 
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe6 in position 0: ordinal not in range(128)
>>> unicode(s,encoding='utf-8')
u'\u6211wo'
>>> u=u'我wo'
>>> str(u)
Traceback (most recent call last):
  File "", line 1, in 
UnicodeEncodeError: 'ascii' codec can't encode character u'\u6211' in position 0: ordinal not in range(128)
>>> str(u,encoding='utf-8')
Traceback (most recent call last):
  File "", line 1, in 
TypeError: str() takes at most 1 argument (2 given)
>>> u.str()
Traceback (most recent call last):
  File "", line 1, in 
AttributeError: 'unicode' object has no attribute 'str'
>>> u.encode('utf-8')
'\xe6\x88\x91wo'



你可能感兴趣的:(Python)