the_Rings
the_Rings
2017-12-04 01:24

python中的编码与字典中的编码,迷惑不解

  • python
  • 编码
  • dict
  • unicode
  • 字典
IPython 5.4.1 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.

In [1]: a = {1: '我爱吃鱼'}

In [2]: a
Out[2]: {1: '\xe6\x88\x91\xe7\x88\xb1\xe5\x90\x83\xe9\xb1\xbc'}

In [3]: a[1]
Out[3]: '\xe6\x88\x91\xe7\x88\xb1\xe5\x90\x83\xe9\xb1\xbc'

In [4]: print a[1]
我爱吃鱼

In [5]: b = {1: u'我爱吃鱼'}

In [6]: b
Out[6]: {1: u'\u6211\u7231\u5403\u9c7c'}

In [7]: print(b[1])
我爱吃鱼

In [8]: 

python2.7使用ipython编辑,为什么字典中的汉字是ascii码,unicode进行存储,而打印出来就不一样呢?大神解释一下上述过程。

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

2条回答