use*_*150 5 python utf-8 windows-console python-2.7
我有以下代码:
# -*- coding: utf-8 -*-
print u"William Burges (1827–81) was an English architect and designer."
Run Code Online (Sandbox Code Playgroud)
当我尝试从 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>
Run Code Online (Sandbox Code Playgroud)
我怎样才能解决这个问题并使 Python 读取这个 \u2013 字符?以及为什么 Python 不使用现有代码读取它,我认为 utf-8 适用于每个字符。
谢谢
编辑:
此代码打印出想要的结果:
# -*- coding: utf-8 -*-
print unicode("William Burges (1827-81) was an English architect and designer.", "utf-8").encode("cp866")
Run Code Online (Sandbox Code Playgroud)
但是当我尝试打印多个句子时,例如:
# -*- coding: utf-8 -*-
print unicode("William Burges (1827–81) was an English architect and designer. I am here. ", "utf-8").encode("cp866")
Run Code Online (Sandbox Code Playgroud)
我收到相同的错误消息:
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>
Run Code Online (Sandbox Code Playgroud)
我怀疑问题出在 print 语句上,而不是 python 固有的任何东西(它在我的 Mac 上工作正常)。为了打印字符串,需要将其转换为可显示的格式;您使用的较长破折号在 Windows 命令行的默认字符集中无法显示。
\n\n你的两个句子之间的区别不在于长度,而在于“(1827-81)”与“(1827\xe2\x80\x9381)”中使用的破折号的种类 - 你能看到细微的差别吗?尝试将一个复制并粘贴到另一个上以进行检查。
\n\n另请参阅Python、Unicode 和 Windows 控制台。
\n| 归档时间: |
|
| 查看次数: |
27936 次 |
| 最近记录: |