简体   繁体   English

为什么在控制台中运行Python脚本而不在Eclipse / PyDev中运行时会引发UnicodeDecodeError?

[英]Why is a UnicodeDecodeError raised when running Python script in console, but not in Eclipse/PyDev?

My script raises a UnicodeDecodeError when run in the Windows 8 console, but not when run in Eclipse/PyDev as a launch configuration. 在Windows 8控制台中运行时,我的脚本引发UnicodeDecodeError,但是在Eclipse / PyDev中作为启动配置运行时,则不会引发UnicodeDecodeError。 Where is the difference between the PyDev environment and running python.exe from the console in regards to unicode? 在Unicode方面,PyDev环境与从控制台运行python.exe的区别在哪里?

The difference is the default encoding. 区别在于默认编码。 You can retrieve this via sys.getdefaultencoding() . 您可以通过sys.getdefaultencoding()检索它。 You will notice that the default encoding in a Windows 8 shell is ascii , while PyDev defaults a launch configuration's default encoding to the project's default encoding, which itself defaults to Cp1252 . 您会注意到,Windows 8 Shell中的默认编码为ascii ,而PyDev将启动配置的默认编码默认为项目的默认编码,后者本身默认为Cp1252

There are a few (problematic) tricks to change in the default encoding in code, primarily reload(sys); sys.setdefaultencoding('myencoding') 有一些(问题)技巧可以更改代码的默认编码,主要是reload(sys); sys.setdefaultencoding('myencoding') reload(sys); sys.setdefaultencoding('myencoding') . reload(sys); sys.setdefaultencoding('myencoding') Ian Bicking documented this in his blog post The Illusive setdefaultencoding . Ian Bicking在他的博客文章The Illusive setdefaultencoding中对此进行了记录。

If you just want to align the PyDev behavior to what you see in the console (or on your production servers), you can change the default encoding on the 'Common' tab of the launch configuration properties: 如果只想使PyDev行为与控制台(或生产服务器)上看到的一致,则可以在启动配置属性的“常用”选项卡上更改默认编码:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM