改变Python的默认编码? [英] Changing default encoding of Python?

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

问题描述

我有很多无法编码和无法解码的问题,当我从控制台运行我的应用程序时,Python 。但是在 Eclipse PyDev IDE,默认字符编码设置为 UTF-8 ,我很好。

I have many "can't encode" and "can't decode" problems with Python when I run my applications from the console. But in the Eclipse PyDev IDE, the default character encoding is set to UTF-8, and I'm fine.

我搜索了设置默认编码,人们说Python删除了code> sys.setdefaultencoding 函数启动时,我们不能使用它。

I searched around for setting the default encoding, and people say that Python deletes the sys.setdefaultencoding function on startup, and we can not use it.

那么最好的解决方案是什么? p>

So what's the best solution for it?

推荐答案

这是一个更简单的方法(黑客),让你回来 setdefaultencoding() sys 中删除​​的函数:

Here is a simpler method (hack) that gives you back the setdefaultencoding() function that was deleted from sys:

import sys
# sys.setdefaultencoding() does not exist, here!
reload(sys)  # Reload does the trick!
sys.setdefaultencoding('UTF8')

这不是一件安全的事情要做,虽然:这显然是一个黑客,因为 sys.setdefaultencoding()有意从 sys Python启动时。重新启用并更改默认编码可以中断代码这依赖于ASCII是默认的(此代码可以是第三方,这通常会使其不可能或不危险)。

This is not a safe thing to do, though: this is obviously a hack, since sys.setdefaultencoding() is purposely removed from sys when Python starts. Reenabling it and changing the default encoding can break code that relies on ASCII being the default (this code can be third-party, which would generally make fixing it impossible or dangerous).

这篇关于改变Python的默认编码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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