【问题标题】:How to show "±" in python?如何在python中显示“±”?
【发布时间】:2017-06-10 06:31:52
【问题描述】:

只是一个示例问题。是否可以在 Python 中显示符号 ±? 我刚刚尝试使用扩展 ASCII 码 char(241),但没有成功。

简单代码:

b = chr(241)    #The Extended ASCII Codes for 241 is this symbol "±".
print(b)

结果显示UnicodeEncodeError: 'ascii' codec can't encode character '\x80' in position 0: ordinal not in range(128)

有人可以帮忙吗?谢谢

我只是想测试一下是否可以在 Python 中显示扩展 ASCII 代码。没有复杂的代码。

这是代码和结果:

【问题讨论】:

  • 能否请您显示更多您的代码,这将有助于调试您的错误。
  • print('±') 有什么问题?
  • 我也在运行 3.6.1,它对我来说很好用。检查 Sublime 文档以获取控制台设置,或从支持正确字符集的命令行运行程序。
  • @cdarke 知道了。它适用于命令行。

标签: python ascii extended-ascii


【解决方案1】:

总结一下 cmets,可以应用以下任何一种替代方案

print('±') # Provided your terminal supports Unicode
print('\xB1') # For all cases.

【讨论】:

  • print('\xB1') # For all cases
  • @JosefZ 谢谢你的建议,我已经做了必要的修改。
猜你喜欢
  • 1970-01-01
  • 2020-01-26
  • 1970-01-01
  • 1970-01-01
  • 2013-12-26
  • 2020-08-09
  • 2017-05-10
  • 2011-05-08
  • 2021-12-20
相关资源
最近更新 更多