Python 3.6的更改其他一些默认值:但是 open()的默认编码仍然是Python设法从环境中推断出来的.似乎3.7将添加一个(选择加入!)模式,其中环境语言环境编码将被忽略,并且所有内容始终都是UTF-8(我想除非Windows使用UTF-16的特定情况除外).参见 PEP 0540 和相应的问题29240 .I recently ran into some problems decoding a handle (with errors mapping 0x81, 0x8D) from the Biopython module with an anaconda 4.1.1 python 3.5.2 installation on a sony vaio windows 10 systemAfter some research, it seems that possibly the problem may be that the default decoding codec is cp1252. I ran the code below and found that indeed the default codec is set to cp1252.However, several posts suggest that python 3 should have set the default codec to utf8. Is that correct? If so, why is mine cp1252 and how can I solve this?import localeos_encoding = locale.getpreferredencoding() 解决方案 According to What’s New In Python 3.0,andIn other words, Python opens source files as UTF-8 by default, but any interaction with the filesystem will depend on the environment. It's strongly recommended to use open(filename, encoding='utf-8') to read a file.Another change is that b'bytes'.decode() and 'str'.encode() with no argument use utf-8 instead of ascii.Python 3.6 changes some more defaults:But the default encoding for open() is still whatever Python manages to infer from the environment.It appears that 3.7 will add an (opt-in!) mode where the environmental locale encoding is ignored, and everything is all UTF-8 all the time (except for specific cases where Windows uses UTF-16, I suppose). See PEP 0540 and corresponding Issue 29240. 这篇关于Python 3默认编码cp1252的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
09-13 13:11
查看更多