【问题标题】:python 2.7 character \u2013 [duplicate]python 2.7字符\u2013 [重复]
【发布时间】:2013-12-18 06:07:57
【问题描述】:

我有以下代码:

# -*- coding: utf-8 -*-

print u"William Burges (1827–81) was an English architect and designer."

当我尝试从 cmd 运行它时。我收到以下消息:

Traceback (most recent call last):
  File "C:\Python27\utf8.py", line 3, in <module>
    print u"William Burges (1827ŌĆō81) was an English architect and designer."
  File "C:\Python27\lib\encodings\cp775.py", line 12, in encode
    return codecs.charmap_encode(input,errors,encoding_map)
UnicodeEncodeError: 'charmap' codec can't encode character u'\u2013' in position
 20: character maps to <undefined>

我怎样才能解决这个问题并让 Python 读取这个 \u2013 字符?以及为什么 Python 不使用现有代码读取它,我认为 utf-8 适用于每个字符。

谢谢

编辑:

这段代码打印出想要的结果:

# -*- coding: utf-8 -*-

print unicode("William Burges (1827-81) was an English architect and designer.", "utf-8").encode("cp866")

但是当我尝试打印多个句子时,例如:

# -*- coding: utf-8 -*-

print unicode("William Burges (1827–81) was an English architect and designer. I am here. ", "utf-8").encode("cp866")

我收到同样的错误信息:

Traceback (most recent call last):
  File "C:\Python27\utf8vs.py", line 3, in <module>
    print unicode("William Burges (1827ŌĆō81) was an English architect and desig
ner. I am here. ", "utf-8").encode("cp866")
  File "C:\Python27\lib\encodings\cp866.py", line 12, in encode
    return codecs.charmap_encode(input,errors,encoding_map)
UnicodeEncodeError: 'charmap' codec can't encode character u'\u2013' in position
 20: character maps to <undefined>

【问题讨论】:

标签: python python-2.7 utf-8 windows-console


【解决方案1】:

我怀疑问题出在打印语句上,而不是 python 固有的任何东西(它在我的 Mac 上运行良好)。为了打印字符串,需要将其转换为可显示的格式;您使用的较长破折号在 Windows 命令行的默认字符集中无法显示。

您的两个句子之间的区别不在于长度,而在于“(1827-81)”与“(1827-81)”中使用的破折号-您能看出细微的区别吗?尝试复制并粘贴一个在另一个上进行检查。

另见Python, Unicode, and the Windows console

【讨论】:

    【解决方案2】:

    在 wiki.python.org 上实际上有一篇关于此问题 https://wiki.python.org/moin/PrintFails 的 wiki 文章解释了为什么 charmap 编解码器可能会发生这种情况。

    Setting the PYTHONIOENCODING environment variable as described above can be used to suppress the error messages. Setting to "utf-8" is not recommended as this produces an inaccurate, garbled representation of the output to the console. For best results, use your console's correct default codepage and a suitable error handler other than "strict".

    【讨论】:

      【解决方案3】:

      您的字符串包含 ndash sumbol。它类似于 ascii 减去 -,参见符号 No 45 和 ascii table。将 ndash 替换为减号,因为 ascii 不能包含 ndash。下面的工作变体:

      # -*- coding: utf-8 -*-
      
      my_string = "William Burges (1827–81) was an English architect and designer."
      my_string = my_string.replace("–", "-")# replace utf-8 symbol (ndash) to ascii (-)
      print my_string
      

      输出

      William Burges (1827-81) was an English architect and designer. I am here. 
      

      【讨论】:

        猜你喜欢
        • 2018-09-27
        • 1970-01-01
        • 2016-03-10
        • 1970-01-01
        • 2013-08-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多