Python中的Unicode显示

2024-09-26 17:42:05 发布

您现在位置:Python中文网/ 问答频道 /正文

The Unicode standard describes how characters are represented by code points and contains a lot of tables listing characters and their corresponding code points:

0061    'a'; LATIN SMALL LETTER A
0062    'b'; LATIN SMALL LETTER B

From https://docs.python.org/2/howto/unicode.html#definitions

在Python中,字符有两种不同的显示形式,这两种形式是相等的:

u'中文' == u'\u4e2d\u6587'

显然,人类想要阅读u'中文',而不是u'\u4e2d\u6587'。但在Python2的某些情况下,unicode仅显示为unicode点:

>>> print(u'\u4e2d\u6587')
中文
>>> print({u'\u4e2d\u6587': 1})
{u'\u4e2d\u6587': 1}
>>> print([u'\u4e2d\u6587', 1])
[u'\u4e2d\u6587', 1]

但是Python3没有问题

>>> print({u'\u4e2d\u6587': 1})
{'中文': 1}
>>> print([u'\u4e2d\u6587', 1])
['中文', 1]

以下是我的问题:

  • 我能告诉Python我想要哪种unicode显示形式吗
  • 为什么Python3没有问题
  • Python2有简单的解决方案吗

我在以下链接中没有找到好的解决方案:


Tags: andhtmlunicodecode形式pointssmallprint

热门问题