在 Windows 上设置 Python 语言环境的正确方法是什么? [英] What is the correct way to set Python's locale on Windows?
问题描述
我正在尝试以区域设置感知方式对字符串列表进行排序.我已经将 Babel 库用于其他与 i18n 相关的任务,但它不支持排序.Python 的 locale
模块提供了一个 strcoll
函数,但需要将进程的语言环境设置为我想要使用的语言环境.有点痛,但我可以忍受.
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. 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. Kind of a pain, but I can live with it.
问题是我似乎无法实际设置语言环境.locale
模块的 documentation 给出了这个例子:>
The problem is that I can't seem to actually set the locale. The documentation for the locale
module gives this example:
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:Python26Liblocale.py", line 494, in setlocale
locale.Error: unsupported locale setting
我做错了什么?
推荐答案
您似乎在使用 Windows.那里的语言环境字符串不同.更精确地查看文档:
It seems you're using 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
在 Windows 上,我认为应该是这样的:
On Windows, I think it would be something like:
locale.setlocale(locale.LC_ALL, 'deu_deu')
MSDN has a list of language strings and of country/region strings
这篇关于在 Windows 上设置 Python 语言环境的正确方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!