如何使用打印在 Python 中显示特殊字符 [英] How to display special characters in Python with print

查看:67
本文介绍了如何使用打印在 Python 中显示特殊字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我正在编写的 Python 程序中,我需要打印 ©(版权)符号.是否有捷径可寻?还是 Python 不支持?这是一个例子.

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.

print ("\(copyright symbol here\)") 

只是一个非常简单的问题.谢谢!

Just a very simple problem. Thanks!

推荐答案

在 Python 中,您可以通过三种方式将 Unicode 字符放入字符串中.(如果您使用 2.x 而不是 3.x,则使用 Unicode 字符串更简单——如在 u"..." 中而不是 "..." ——和你必须使用 unichr 而不是 chr,否则一切都是一样的.)

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 unichr instead of chr, but otherwise everything is the same.)

  • '©':直接输入.
    • 这意味着您可能必须为源代码选择一种字符编码——例如,将文件明确保存为 UTF-8,并在其顶部放置一个编码标头.(对于 3.3,UTF-8 是默认值,因此如果您使用的是编码标头,则不需要.)
    • 在 Mac OS X 下,在大多数语言的默认键盘设置中,这是 opt-G.
    • 在 Windows 下,我相信您可以使用带有 0169 的 alt-numeric-keypad 技巧来输入它,尽管这看起来并不容易.
    • 如果您不知道如何用键盘输入©",请从其他地方复制并粘贴它(谷歌版权标志",您应该找到一个可以复制它的页面——或者,就此而言,正确的从这里开始).
    • 或者,您的计算机可能有一个字符查看器或类似的东西,可以让您指向和单击特殊字符.
    • 在 Google 上搜索unicode copyright sign",您很快就会发现它是 U+00A9.在 Python 中,它是 `'\u00a9'.
    • 对于基本多语言平面之外的任何内容,即超过 4 个十六进制数字,请使用大写的 U 和 8 个数字.
    • 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 U and 8 digits.
    • 同样,您可能需要通过谷歌搜索为实体找到正确的名称.
    • 您可以使用和不可以使用哪些名称并没有完全记录下来.但它通常会在您期望的时候起作用,而且 COPYRIGHT SIGN 显然比 00a9 更具可读性.
    • 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 SIGN is obviously more readable than 00a9.

    你也可以间接地做一些事情——例如,unicodedata.lookup('COPYRIGHT SIGN')chr(0xa9) 将返回与上述文字相同的字符串.但真的没有理由不使用文字.

    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.

    Python 文档中的 Unicode HOWTO 对此有更多详细信息——如果你不愿意阅读整篇文章,字符串Type 描述了不同类型的转义序列(以及 unicode 和 bytes 字符串之间的编码/解码问题,这在 2.x 中尤为重要)和 Python 源代码中的 Unicode 文字 描述了如何指定编码声明.

    The Unicode HOWTO in the Python docs has a lot more detail on this—if you're not willing to read the whole thing, The String Type describes 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 Code describes how to specify a coding declaration.

    如果你想要一个你可以使用的所有字符的官方列表,而不是仅仅在谷歌上搜索它们,请查看 unicodedata 适用于您的 Python 版本的文档,其中包含指向适当版本的 Unicode 字符数据库的链接.(例如,它在 3.3.0 中是 6.1.0,在 2.7.3 中是 5.2.0,等等.)您必须浏览一些链接才能到达实际列表,但这是唯一的方式得到一些保证与编译成 Python 的内容完全相同的内容.(而且,如果你不关心这个,你不妨谷歌一下,或者使用维基百科或你电脑的字符查看器.)

    If you want an official list of all characters you can use, instead of just googling for them, look at the unicodedata docs 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.)

    这篇关于如何使用打印在 Python 中显示特殊字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆