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

问题描述

限时删除!!

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

 如果__name__ ==__main__:
s ='ß'
打印

运行它我收到以下错误:

$ b

 追溯(最近的最后一次呼叫):
文件.\tmp.py,第3行,在< module>
打印
文件C:\Python32\lib\encodings\cp866.py,第19行,编码
return codecs.charmap_encode(input,self。错误,encoding_map)[0]
UnicodeEncodeError:'charmap'编解码器无法在位置0中编码字符\xdf:字符映射到< undefined>

Python文档:

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

如果我把:

 # -  *  -  encoding: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行,编码
return codecs.charmap_encode(输入,错误,encoding_map)
UnicodeEncodeError:'charmap'编解码器无法在位置0中编码字符\xdf:字符映射到< undefined>

Python正在处理它,它的输出编码无法处理。



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



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

 >>> print('\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 ".\tmp.py", line 3, in <module>
    print(s)
  File "C:\Python32\lib\encodings\cp866.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:

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编码的源文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 08:29