改变“区域设置优选编码” [英] Changing the "locale preferred encoding"

查看:99
本文介绍了改变“区域设置优选编码”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

[使用Python 3.2]

[Using Python 3.2]

如果我不向 encoding > open ,则使用 locale.getpreferredencoding()打开文件。例如,在我的Windows机器上,任何时候我使用 open('abc.txt'),它将使用 cp1252

If I don't provide encoding argument to open, the file is opened using locale.getpreferredencoding(). So for example, on my Windows machine, any time I use open('abc.txt'), it would be decoded using cp1252.

我想将所有输入文件切换为 utf-8 。显然,我可以添加 encoding ='utf-8'到所有我的打开函数调用。或者,更好的是, encoding = MY_PROJECT_DEFAULT_ENCODING ,其中常量定义在全局级别。

I would like to switch all my input files to utf-8. Obviously, I can add encoding = 'utf-8' to all my open function calls. Or, better, encoding = MY_PROJECT_DEFAULT_ENCODING, where the constant is defined at the global level somewhere.

想知道是否有一个干净的方式,以避免编辑所有的打开调用,通过更改默认编码。是我可以通过更改区域设置更改的东西?或者通过更改区域设置中的参数?我尝试按照Python手册,但无法理解应该如何使用。

But I was wondering if there is a clean way to avoid editing all my open calls, by changing the "default" encoding. Is it something I can change by changing the locale? Or by changing a parameter inside the locale? I tried to follow the Python manual but failed to understand how this is supposed to be used.

谢谢!

推荐答案

在Windows中,使用Python 3.3+,在运行Python之前在控制台或批处理文件中执行 chcp 65001 区域设置编码为UTF-8。

In Windows, with Python 3.3+, execute chcp 65001 in the console or a batch file before running Python in order to change the locale encoding to UTF-8.

这篇关于改变“区域设置优选编码”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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