代码改变世界

python 编码问题

2018-06-21 21:15  懦米虫  阅读(199)  评论(0编辑  收藏  举报

>>> a="中"

>>> a
'\xe4\xb8\xad'
>>> b=a.decode()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe4 in position 0: ordinal not in range(128)
>>> b=a.encode("utf-8")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe4 in position 0: ordinal not in range(128)
>>> b=a.decode("utf-8")
>>> b
u'\u4e2d'
>>> a=u"中"
>>> a
u'\u4e2d'

怎么样才能出来中文字?