python - 无法在 Python 2 中打印 Unicode 比特币符号

标签 python unicode

我正在尝试在 Python 2 中打印 Unicode 比特币符号 \u2043。我尝试添加 #-*- coding: utf-8 -*-

$ python2 -c 'print u'\u0243''

引发 UnicodeEncodeError:'ascii' 编解码器无法对位置 0 中的字符 u'\u0243' 进行编码:序号不在范围内 (128)。但是,从 Python shell 执行此操作是可行的。

$ python2
>>> print u'\u0243'
Ƀ

为什么这段代码不起作用?

bitcoin = u'\u0243'
quote = u'{:,.2f}'.format(float(val), '.2f')
print bitcoin, quote

最佳答案

在 Unix 上,如果 sys.stdout.isatty() 返回 Truesys.stdout.encoding'ANSI_X3。 4-1968' (ascii) 那么你应该配置你的语言环境(检查 LANG, LC_CTYPE, LC_ALL envvars)使用非-ascii编码,如果你需要打印非ascii字符。

如果 sys.stdout.isatty() 为 false,则在脚本外配置 PYTHONIOENCODING envvar。
始终打印 Unicode,不要在脚本中硬编码环境的字符编码。

关于python - 无法在 Python 2 中打印 Unicode 比特币符号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33054410/

相关文章:

delphi - 用Delphi查找系统字体

运行 mysql 查询时的 Python 'TypeError: not enough arguments for format string'

python - 在pycharm中连接html文件和css文件

python - 将两个列表组合在字典中 - python

unicode - 在 Fantasyvrb 的 VerbatimOut 中使用 Unicode

python - iPython:gnome 终端(linux)中的 unicode

java - 如何将文件读入 UTF-8 Java 应用程序,其中文件采用不同的字符编码?

python - 如何使用 Wand 更改图片的对比度?

python - 如何从两个嵌套列表制作字典?

python - 在事先不知道代码页的情况下将原始字节字符串转换为 Unicode