为什么 Python 不能识别我的 utf-8 编码源文件? [英] Why doesn't Python recognize my utf-8 encoded source file?

查看:30
本文介绍了为什么 Python 不能识别我的 utf-8 编码源文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个带有非 ASCII 字符的小 tmp.py:

if __name__ == "__main__":s = 'ß'印刷)

运行时出现以下错误:

回溯(最近一次调用最后一次):文件.	mp.py",第 3 行,在 <module> 中印刷)文件C:Python32libencodingscp866.py",第 19 行,编码返回 codecs.charmap_encode(input,self.errors,encoding_map)[0]UnicodeEncodeError: 'charmap' 编解码器无法对位置 0 的字符 'xdf' 进行编码:字符映射到 <undefined>

Python 文档:

<块引用>

默认情况下,Python 源文件被视为以 UTF-8 编码...

我检查编码的方法是使用 Firefox(也许有人会建议一些更明显的东西).我在 Firefox 中打开 tmp.py,如果我选择 View->Character Encoding->Unicode (UTF-8) 它看起来没问题,这就是它在这个问题上面的样子(wth ß符号).

如果我把:

# -*- 编码:utf-8 -*-

作为 tmp.py 中的第一个字符串,它不会改变任何东西——错误仍然存​​在.

有人能帮我弄清楚我做错了什么吗?

解决方案

终端使用的编码没有支持那个角色:

<预><代码>>>>'xdf'.encode('cp866')回溯(最近一次调用最后一次):文件<stdin>",第 1 行,在 <module> 中文件/opt/local/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/encodings/cp866.py",第12行,编码返回 codecs.charmap_encode(input,errors,encoding_map)UnicodeEncodeError: 'charmap' 编解码器无法对位置 0 的字符 'xdf' 进行编码:字符映射到 <undefined>

Python 处理得很好,是您的输出编码无法处理它.

您可以尝试在 Windows 控制台中使用 chcp 65001 来切换您的代码页;chcp 是用于更改代码页的 Windows 命令行命令.

我的,在 OS X 上(使用 UTF-8)可以很好地处理它:

<预><代码>>>>打印('xdf')ß

Here is a little tmp.py with a non ASCII character:

if __name__ == "__main__":
    s = 'ß'
    print(s)

Running it I get the following error:

Traceback (most recent call last):
  File ".	mp.py", line 3, in <module>
    print(s)
  File "C:Python32libencodingscp866.py", line 19, in encode
    return codecs.charmap_encode(input,self.errors,encoding_map)[0]
UnicodeEncodeError: 'charmap' codec can't encode character 'xdf' in position 0: character maps to <undefined>

The Python docs says:

By default, Python source files are treated as encoded in UTF-8...

My way of checking the encoding is to use Firefox (maybe someone would suggest something more obvious). I open tmp.py in Firefox and if I select View->Character Encoding->Unicode (UTF-8) it looks ok, that is the way it looks above in this question (wth ß symbol).

If I put:

# -*- encoding: utf-8 -*-

as the first string in tmp.py it does not change anything—the error persists.

Could someone help me to figure out what am I doing wrong?

解决方案

The encoding your terminal is using doesn't support that character:

>>> 'xdf'.encode('cp866')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/encodings/cp866.py", line 12, in encode
    return codecs.charmap_encode(input,errors,encoding_map)
UnicodeEncodeError: 'charmap' codec can't encode character 'xdf' in position 0: character maps to <undefined>

Python is handling it just fine, it's your output encoding that cannot handle it.

You can try using chcp 65001 in the Windows console to switch your codepage; chcp is a windows command line command to change code pages.

Mine, on OS X (using UTF-8) can handle it just fine:

>>> print('xdf')
ß

这篇关于为什么 Python 不能识别我的 utf-8 编码源文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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