我正在尝试打印给定名称的 unicode 字符,如下所示:
# -*- coding: utf-8 -*-
print "\N{SOLIDUS}"
print "\N{BLACK SPADE SUIT}"
然而我得到的输出并不是很令人鼓舞。
转义序列按原样打印。
ActivePython 2.7.2.5 (ActiveState Software Inc.) based on
Python 2.7.2 (default, Jun 24 2011, 12:21:10) [MSC v.1500 32 bit (Intel)] on
Type "help", "copyright", "credits" or "license" for more information.
>>> # -*- coding: utf-8 -*-
... print "\N{SOLIDUS}"
\N{SOLIDUS}
>>> print "\N{BLACK SPADE SUIT}"
\N{BLACK SPADE SUIT}
>>>
然而我可以看到另一个提问者 https://stackoverflow.com/questions/13106175/how-to-find-out-number-name-of-unicode-character-in-python%20another%20asker已经能够成功地做到这一点。
怎么了?
这些序列仅适用于 unicode 字符串 http://docs.python.org/2/reference/lexical_analysis.html#string-literals,这是 Python 3 拥有的唯一一种字符串。所以在 Python 2 中你需要在字符串前面加上前缀u
.
>>> print "\N{SOLIDUS} \N{BLACK SPADE SUIT}"
\N{SOLIDUS} \N{BLACK SPADE SUIT}
>>> print u"\N{SOLIDUS} \N{BLACK SPADE SUIT}"
/ ♠
文档中的相关行:
\N{name}
Unicode 数据库中名为 name 的字符(仅限 Unicode)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)