[英]What is the correct way to set Python's locale on Windows?
I'm attempting to sort a list of strings in a locale-aware manner.我正在尝试以区域设置感知方式对字符串列表进行排序。 I've used the Babel library for other i18n-related tasks, but it doesn't support sorting.
我已经将 Babel 库用于其他与 i18n 相关的任务,但它不支持排序。 Python's
locale
module provides a strcoll
function, but requires the locale of the process to be set to the one I want to work with. Python 的
locale
模块提供了一个strcoll
函数,但需要将进程的语言环境设置为我想要使用的语言环境。 Kind of a pain, but I can live with it.有点痛苦,但我可以忍受。
The problem is that I can't seem to actually set the locale.问题是我似乎无法实际设置语言环境。 The documentation for the
locale
module gives this example: locale
模块的文档给出了这个例子:
import locale
locale.setlocale(locale.LC_ALL, 'de_DE')
When I run that, I get this:当我运行它时,我得到了这个:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python26\Lib\locale.py", line 494, in setlocale
locale.Error: unsupported locale setting
What am I doing wrong?我究竟做错了什么?
It seems you're using Windows.看来您正在使用 Windows。 The locale strings are different there.
那里的语言环境字符串不同。 Take a more precise look at the doc:
更精确地看一下文档:
locale.setlocale(locale.LC_ALL, 'de_DE') # use German locale; name might vary with platform
On Windows, I think it would be something like:在 Windows 上,我认为它会是这样的:
locale.setlocale(locale.LC_ALL, 'deu_deu')
MSDN has a list of language strings and of country/region strings MSDN 有一个语言字符串列表和国家/地区字符串列表
You should not pass an explicit locale to setlocale, it is wrong.您不应将显式语言环境传递给 setlocale,这是错误的。 Let it find out from the environment.
让它从环境中发现。 You have to pass it an empty string
你必须传递一个空字符串
import locale
locale.setlocale(locale.LC_ALL, '')
This is the only correct way to use it, providing an example for the German locale:这是使用它的唯一正确方法,为德语语言环境提供示例:
import locale
locale.setlocale(
category=locale.LC_ALL,
locale="German") # Note: do not use "de_DE" as it doesn't work
On Ubuntu you may have this problem because you don't have that local installed on your system.在 Ubuntu 上,你可能会遇到这个问题,因为你的系统上没有安装本地。
From shell try a:从 shell 尝试:
$> locale -a
and check if you find the locale you are interested in. Otherwise you have to install it:并检查您是否找到了您感兴趣的语言环境。否则您必须安装它:
$> sudo apt-get install language-pack-XXX
where XXX is your language (in my case "xxx = it" , italian locale) Then run a dpkg-reconfigure
:其中 XXX 是您的语言(在我的情况下为 "xxx = it" ,意大利语语言环境)然后运行
dpkg-reconfigure
:
$> sudo dpkg-reconfigure locales
After that try again in your python shell:之后在你的 python shell 中再试一次:
>>> import locale
>>> locale.setlocale(locale.LC_ALL,'it_IT.UTF-8')
(this is for italian locale, which was what I needed) (这是针对意大利语环境的,这正是我所需要的)
I know this has been asked years ago, but I thought I'd try adding what I found out, using Python 3.6 on Windows:我知道几年前就有人问过这个问题,但我想我会尝试添加我发现的内容,在 Windows 上使用 Python 3.6:
import locale
for x in locale.windows_locale.values():
print(x.replace('_','-'))
I tried some and that also seems to be a way of finding out, what's available on Windows.我尝试了一些,这似乎也是一种找出 Windows 上可用内容的方法。
Good to know: This for some reason is not compatible to strptime() at the current stable version of Python很高兴知道:出于某种原因,这与当前稳定版本的 Python 中的 strptime() 不兼容
And then you simply set the locale:然后您只需设置语言环境:
locale.setlocale(locale.LC_ALL, any_item_of_the_printed_strings)
From locale.setlocale docs:来自 locale.setlocale 文档:
locale.setlocale(category, locale=None):
"""
Set the locale for the given category. The locale can be
a string, an iterable of two strings (language code and encoding),
or None.
""""
Under Linux (especially Ubuntu) you can either use在 Linux(尤其是 Ubuntu)下,您可以使用
locale.setlocale(locale.LC_ALL, 'de_DE.UTF-8')
or或者
locale.setlocale(locale.LC_ALL, ('de', 'utf-8'))
You will get the same error if the locale is not installed on the system.如果系统上未安装语言环境,您将收到相同的错误。 So, make sure you have the locale installed on your system:
因此,请确保您的系统上安装了语言环境:
$ locale -a # to list the currently installed locales
$ (sudo) locale-gen de_DE.UTF-8 # to install new locale
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.