Python 3默认编码cp1252 [英] Python 3 Default Encoding cp1252

查看:103
本文介绍了Python 3默认编码cp1252的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近遇到了一些问题,即在索尼vaio Windows 10系统上安装了Anaconda 4.1.1 python 3.5.2的Biopython模块从句柄中解码句柄(错误映射0x81、0x8D)

经过一些研究,似乎可能是默认解码编解码器是cp1252的问题.我运行了以下代码,发现确实将默认编解码器设置为cp1252.

但是,有几篇文章建议python 3应该将默认编解码器设置为utf8.那是对的吗?如果是这样,为什么是我的cp1252,我该如何解决?导入语言环境os_encoding = locale.getpreferredencoding()

解决方案

根据新增功能在Python 3.0中

有一个依赖于平台的默认编码[…]在很多情况下(但不是全部),系统默认值为UTF-8.您永远不要指望这个默认值.

PEP 3120:现在默认的源编码为UTF-8.

换句话说,Python默认将源文件打开为UTF-8,但是与文件系统的任何交互都将取决于环境.强烈建议使用 open(filename,encoding ='utf-8')来读取文件.

另一个变化是不带参数的 b'bytes'.decode()'str'.encode()使用utf-8而不是ascii.

Python 3.6的更改其他一些默认值:

PEP 529:将Windows文件系统编码更改为UTF-8

PEP 528:将Windows控制台编码更改为UTF-8

但是 open()的默认编码仍然是Python设法从环境中推断出来的.

似乎3.7将添加一个(选择加入!)模式,其中环境语言环境编码将被忽略,并且所有内容始终都是UTF-8(我想除非Windows使用UTF-16的特定情况除外).参见 PEP 0540 和相应的 解决方案

According to What’s New In Python 3.0,

There is a platform-dependent default encoding […] In many cases, but not all, the system default is UTF-8; you should never count on this default.

and

PEP 3120: The default source encoding is now UTF-8.

In 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:

PEP 529: Change Windows filesystem encoding to UTF-8

PEP 528: Change Windows console encoding to UTF-8

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

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