如何使用打印在 Python 中显示特殊字符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15102222/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to display special characters in Python with print
提问by Evan
In a Python program that I am writing, I need to print the ? (copyright) symbol. Is there an easy way to do this? Or is it not supported in Python? Here's an example.
在我正在编写的 Python 程序中,我需要打印 ? (版权)符号。是否有捷径可寻?还是 Python 不支持?这是一个例子。
print ("\(copyright symbol here\)")
Just a very simple problem. Thanks!
只是一个非常简单的问题。谢谢!
回答by Ry-
Sure! Type the copyright symbol:
当然!输入版权符号:
print("?")
(There aren't character entities in Python like there are in, say, HTML.)
(Python 中没有像 HTML 中那样的字符实体。)
回答by unutbu
The copyright sign is a unicode character. If your terminal supports a character encoding (such as utf-8 or cp1252) that includes this character, then you can print it:
版权标志是一个 unicode 字符。如果您的终端支持包含此字符的字符编码(例如 utf-8 或 cp1252),则您可以打印它:
This relies on Python detecting the terminal's character encoding:
这依赖于 Python 检测终端的字符编码:
In [64]: print(u'\N{COPYRIGHT SIGN}')
?
This uses an explicit encoding (which happens to work since my terminal is set to use the utf-8 character encoding):
这使用了显式编码(由于我的终端设置为使用 utf-8 字符编码,因此它恰好可以工作):
In [65]: print(u'\N{COPYRIGHT SIGN}'.encode('utf-8'))
?
回答by abarnert
In Python, you can put Unicode characters inside strings in three ways. (If you're using 2.x instead of 3.x, it's simpler to use a Unicode string—as in u"…"instead of "…"—and you have to use unichrinstead of chr, but otherwise everything is the same.)
在 Python 中,您可以通过三种方式将 Unicode 字符放入字符串中。(如果您使用的是 2.x 而不是 3.x,使用 Unicode 字符串更简单——就像 inu"…"而不是"…"——并且你必须使用unichr而不是chr,但其他一切都是一样的。)
- '?': Type it directly.
- This means you will probably have to pick a character encoding for your source code—e.g., explicitly save the file as UTF-8, and put an encoding header at the top of it. (For 3.3, UTF-8 is the default, so you don't need the encoding header if that's what you use.)
- Under Mac OS X, in most languages' default keyboard setup, this is opt-G.
- Under Windows, I believe you can use the alt-numeric-keypad trick with 0169 to enter it, although that doesn't seem very easy.
- If you don't know how to type '?' with your keyboard, copy and paste it from elsewhere (google "copyright sign" and you should find a page you can copy it from—or, for that matter, right from here).
- Or, your computer probably has a Character Viewer or something similar that lets you point and click at special characters.
- '\u00a9': Use the Unicode numeric escape sequence.
- Google for "unicode copyright sign", and you'll quickly see that it's U+00A9. In Python, that's `'\u00a9'.
- For anything outside the Basic Multilingual Plane—that is, more than 4 hex digits, use a capital
Uand 8 digits.
'\N{COPYRIGHT SIGN}': Use a Unicode entity name escape sequence.- Again, you'll probably need to google to find the right name for the entity.
- It isn't entirely documented what names you can and can't use. But it generally works when you expect it to, and
COPYRIGHT SIGNis obviously more readable than00a9.
- '?':直接输入。
- 这意味着您可能必须为源代码选择一种字符编码——例如,明确地将文件保存为 UTF-8,并在其顶部放置一个编码头。(对于 3.3,UTF-8 是默认值,因此如果您使用的是编码标头,则不需要编码标头。)
- 在 Mac OS X 下,在大多数语言的默认键盘设置中,这是 opt-G。
- 在 Windows 下,我相信您可以使用带有 0169 的 alt-numeric-keypad 技巧来输入它,尽管这看起来并不容易。
- 如果您不知道如何输入“?” 用你的键盘,从别处复制并粘贴它(谷歌“版权标志”,你应该找到一个可以复制它的页面——或者,就此而言,就从这里)。
- 或者,您的计算机可能有一个字符查看器或类似的东西,可以让您指向并单击特殊字符。
- '\u00a9':使用 Unicode 数字转义序列。
- 谷歌搜索“unicode版权标志”,你很快就会看到它是U+00A9。在 Python 中,它是 `'\u00a9'。
- 对于基本多语言平面之外的任何内容——即超过 4 个十六进制数字,使用大写
U和 8 个数字。
'\N{COPYRIGHT SIGN}': 使用 Unicode 实体名称转义序列。- 同样,您可能需要通过谷歌搜索为实体找到正确的名称。
- 它没有完全记录您可以使用和不能使用的名称。但它通常在您期望的时候工作,并且
COPYRIGHT SIGN显然比00a9.
You can also do things indirectly—e.g., unicodedata.lookup('COPYRIGHT SIGN')or chr(0xa9)will return the same string as the literals above. But there's really no reason not to use a literal.
你也可以间接地做一些事情——例如,unicodedata.lookup('COPYRIGHT SIGN')或者chr(0xa9)将返回与上面的文字相同的字符串。但真的没有理由不使用文字。
The Unicode HOWTOin the Python docs has a lot more detail on this—if you're not willing to read the whole thing, The String Typedescribes the different kinds of escape sequences (and the issues with encoding/decoding between unicode and bytes strings, which are especially important in 2.x), and Unicode Literals in Python Source Codedescribes how to specify a coding declaration.
Python 文档中的Unicode HOWTO对此有更多详细信息 - 如果您不愿意阅读整个内容,字符串类型描述了不同类型的转义序列(以及 unicode 和字节字符串之间的编码/解码问题,这在 2.x 中尤为重要),Python 源代码中的Unicode 文字描述了如何指定编码声明。
If you want an official list of all characters you can use, instead of just googling for them, look at the unicodedatadocs for your version of Python, which contains links to the appropriate version of the Unicode Character Database. (For example, it's 6.1.0 in 3.3.0, 5.2.0 in 2.7.3, etc.) You'll have to navigate through a few links to get to the actual list, but this is the only way you'll get something that's guaranteed to be exactly what's compiled into Python. (And, if you don't care about that, you might as well just google it, or use Wikipedia or your computer's character viewer.)
如果你想要一个你可以使用的所有字符的官方列表,而不是仅仅在谷歌上搜索它们,请查看unicodedata你的 Python 版本的文档,其中包含指向适当版本的 Unicode 字符数据库的链接。(例如,它在 3.3.0 中是 6.1.0,在 2.7.3 中是 5.2.0,等等。)您必须浏览一些链接才能到达实际列表,但这是唯一的方式得到一些保证与编译成 Python 的内容完全相同的内容。(而且,如果你不关心这个,你不妨谷歌一下,或者使用维基百科或你电脑的字符查看器。)
回答by yasouser
回答by Ryan Haining
In python 2:
在蟒蛇 2 中:
>>> print u"\u00a9"
?
>>> print u"\N{COPYRIGHT SIGN}"
?
In python 3:
在python 3中:
>>> print("\u00a9")
?
>>> print("\N{COPYRIGHT SIGN}")
?
In python 2 you must prefix the string with a u (u"...") to tell python its a unicode string. However, in python 3 all strings are unicode strings, so you don't have to (and actually aren't allowed to in 3.0-3.2) prefix the string with the u.
在 python 2 中,你必须在字符串前加上 au ( u"...") 来告诉 python 它是一个 unicode 字符串。但是,在 python 3 中,所有字符串都是 unicode 字符串,因此您不必(实际上在 3.0-3.2 中不允许)在字符串前加上 u。
you can view a list of characters and their names / unicode values here: http://www.fileformat.info/info/charset/UTF-16/list.htmand use them the same way you are seeing the copyright symbol used here
您可以在此处查看字符列表及其名称/unicode 值:http: //www.fileformat.info/info/charset/UTF-16/list.htm并以您看到此处使用的版权符号的相同方式使用它们

