在 Python 中使用的 OS 语言环境支持
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1259971/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
OS locale support for use in Python
提问by Yoann Le Touche
The following Python code works on my Windows machine (Python 2.5.4), but doesn't on my Debian machine (Python 2.5.0). I'm guessing it's OS dependent.
以下 Python 代码适用于我的 Windows 机器(Python 2.5.4),但不适用于我的 Debian 机器(Python 2.5.0)。我猜它取决于操作系统。
import locale
locale.setlocale( locale.LC_ALL, 'English_United States.1252' )
I receive the following error:
我收到以下错误:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.5/locale.py", line 476, in setlocale
return _setlocale(category, locale)
locale.Error: unsupported locale setting
Questions:
问题:
- Is it OS dependent?
- How can I find the supported locale list within Python?
- How can I match between Windows locales and Debian locales?
- 它是否依赖于操作系统?
- 如何在 Python 中找到支持的语言环境列表?
- 如何在 Windows 语言环境和 Debian 语言环境之间进行匹配?
回答by Yoann Le Touche
It is OS dependent.
它依赖于操作系统。
To get the list of local available you can use locale -a
in a shell
要获取本地可用列表,您可以locale -a
在 shell 中使用
I think the local you want is something like Windows-1252
我认为你想要的本地是这样的 Windows-1252
回答by pippo2600
try
尝试
apt-get install locales-all
for me it works like a charm
对我来说它就像一个魅力
回答by gimel
Look inside the locale.locale_alias
dictionary.
locale.locale_alias
查字典里面。
>>> import locale
>>> len(locale.locale_alias)
789
>>> locale.locale_alias.keys()[:5]
['ko_kr.euc', 'is_is', 'ja_jp.mscode', 'kw_gb@euro', 'yi_us.cp1255']
>>>
(In my 2.6.2 installation there are 789 locale names.)
(在我的 2.6.2 安装中有 789 个语言环境名称。)
回答by netz75
On Ubuntu Precise type
在 Ubuntu 上精确类型
sudo locale-gen en_US
sudo locale-gen en_US