【发布时间】:2013-01-10 22:35:26
【问题描述】:
为什么我在python中执行此操作时会出现以下错误:
>>> import locale
>>> print str( locale.getlocale() )
(None, None)
>>> locale.setlocale(locale.LC_ALL, 'de_DE')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/locale.py", line 531, in setlocale
return _setlocale(category, locale)
locale.Error: unsupported locale setting
这也适用于 fr 或 nl 等其他语言环境。我使用的是 Ubuntu 11.04。
更新:执行以下操作没有产生任何结果:
dpkg-reconfigure locales
perl: warning: Setting locale failed.
perl: warning: Please check that your locale settings:
LANGUAGE = (unset),
LC_ALL = (unset),
LC_CTYPE = "UTF-8",
LANG = (unset)
are supported and installed on your system.
perl: warning: Falling back to the standard locale ("C").
locale: Cannot set LC_CTYPE to default locale: No such file or directory
locale: Cannot set LC_ALL to default locale: No such file or directory
【问题讨论】:
-
您在
locale -a中列出了de_DE吗?如果没有,你必须install它。 -
相关:bash script 在 Ubuntu 上安装所有语言环境。
-
locale.setlocale(locale.LC_ALL, 'de_DE')是错误的。你需要locale.setlocale(locale.LC_ALL, 'de_DE.utf8')。 -
您有时可以使用内置别名发现您想要的语言/国家/地区的可用编码:
locale.setlocale(locale.LC_ALL, locale.locale_aliases['de_DE'])。 -
我在这里找到了最合适的答案 --> linuxpip.org/fix-python-locale-error
标签: python locale ubuntu-11.04