在Python 3中打印非ASCII字符 [英] Printing non-ascii characters in Python 3

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

问题描述

所以我想打印特殊字符,如®(保留符号字符)。看起来很容易。我找到其他人试图做同样的事情但带有版权符号并赋予它一个旋转,但它错误:

So I want to print special characters like ® (the Reserved Sign char). Seems easy enough. I found someone else trying to do the same thing but with the copyright symbol and gave it a whirl, but it errored out:

Python 3.4.3 (v3.4.3:9b73f1c3e601, Feb 24 2015, 22:43:06) [MSC v.1600 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> print('\u00a9')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python34\lib\encodings\cp437.py", line 19, in encode
    return codecs.charmap_encode(input,self.errors,encoding_map)[0]
UnicodeEncodeError: 'charmap' codec can't encode character '\xa9' in position 0: character maps to <undefined>

我以前在Python 3.4.2上遇到这个问题,只是更新到3.4.3,但它似乎有同样的问题。

I was previously having this issue on Python 3.4.2, and just updated it to 3.4.3, but it seems to be having the same problem.

另一个有趣的事情要注意的是,当我尝试打印这些字符时,python只有错误。如果我没有打印,请:

The other interesting thing to note is that python only errors out when I try to print these characters. If I leave them unprinted, then:

>>> a = '\xae'
>>> if a == '\xae':
...     print(True)
...
True


推荐答案

要打印这些字符,您可以使用:

To print these characters you could use:

ReservedSignChar = '®';
x = ReservedSignChar.encode('utf-8');
print(x);

这篇关于在Python 3中打印非ASCII字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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