设置Python区域设置不起作用 [英] Setting Python locale doesn't work

查看:212
本文介绍了设置Python区域设置不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我一直在设法设置Python语言环境。我想要,因为我想使用当地语言的工作日名称(使用 strftime('%A'))。但是,即使我尝试多种方式设置语言环境,但是目前的平日用英文打印。我正在使用Ubuntu Mate在RPi2上运行。



我已经尝试了这个主题的解决方案设置Python的区域设置的正确方法是什么?



我尝试将locale设置为 nl_NL.utf8

  $> locale -a 
$> python3
>>>导入区域设置
>>> locale.setlocale(locale.LC_ALL,'nl_NL.utf-8')

我还尝试设置空字符串,在最后一个答案中建议:

 >>> locale.setlocale(locale.LC_ALL,'')

在这两种情况下,当我尝试

 >>> locale.getlocale()

我看到 nl_NL.utf8



但是我仍然以英文获得工作日!除了上述主题之外,还没有能够在internetz上找到很多关于这个问题,我无法得到这个修正。






编辑:



我已尝试所有3个选项。第一个返回一个奇怪的结果:

 〜/ Documenten $ python3 
Python 3.4.3+(默认,10月14 2015,16:03:50)
[GCC 5.2.1 20151010] on linux
输入help,copyright,credits或license了解更多信息。
>>> import locale
>>> locale.getlocale(locale.LC_TIME)
(无,无)
>>> locale.setlocale(locale.LC_TIME,'nl_NL.utf-8')
'nl_NL.utf-8'
>>> locale.getlocale(locale.LC_TIME)
('nl_NL','UTF-8')
>>> exit()
jeffrey @ jeffrey-desktop:〜/ Documenten $ python3
Python 3.4.3+(默认,2015年10月14日,16:03:50)
[GCC 5.2.1 20151010 ] on linux
输入help,copyright,credits或license了解更多信息。
>>>导入区域设置
>>> locale.getlocale(locale.LC_TIME)
(无,无)

被保存假设您设置了您的区域设置一次,然后系统会记住这一点,我错了吗?第二个选项:

 〜/ Documenten $ python3 
Python 3.4.3+(默认,2015年10月14日,16 :03:50)
[GCC 5.2.1 20151010] on linux
输入help,copyright,credit或license了解更多信息。
>>>进口日历
>>>> calendar.day_name [2]
'星期三'
>>>导入区域设置
>>> locale.nl_langinfo(locale.DAY_2)
'星期一'

即使我试图得到一个荷兰语的工作日名称,我仍然会得到一个英文名字作为回报。此外,这种方法从星期日开始计数,而不是星期一。我做错了吗?第三个建议给我我的工作日名字,谢谢!但我仍然想知道为什么第一个不起作用?我在自己的设备上使用这个脚本,并希望使用正确的全球区域设置。






Edit2: p>


是的,在Python中调用locale.setlocale()不会影响未来的python进程。请参阅如何设置Ubuntu中的所有区域设置。


确定!所以这就是为什么它不工作。我假设我已经在Python中设置了任何将来使用的区域设置,比如一个环境变量,但是只适用于Python。



我有一个bash脚本创建文件和这个Python脚本应该在一段时间后删除它(学习代码,当我写了Bash脚本时不知道Python)。 Bash的日期方法似乎以某种方式或另一种方式来拾取区域设置。我会继续看看我可以为Python修复什么。这是我在键入 $ locale 时看到的:

  LANG = nl_NL.UTF-8 
LANGUAGE = nl:en
LC_CTYPE =nl_NL.UTF-8
LC_NUMERIC =nl_NL.UTF-8
LC_TIME =nl_NL.UTF -8
LC_COLLATE =nl_NL.UTF-8
LC_MONETARY =nl_NL.UTF-8
LC_MESSAGES =nl_NL.UTF-8
LC_PAPER =nl_NL .UTF-8
LC_NAME =nl_NL.UTF-8
LC_ADDRESS =nl_NL.UTF-8
LC_TELEPHONE =nl_NL.UTF-8
LC_MEASUREMENT = nl_NL.UTF-8
LC_IDENTIFICATION =nl_NL.UTF-8
LC_ALL =


解决方案


似乎没有任何东西被保存。假设您设置了您的区域设置一次,然后系统将记住这个


是的,调用语言环境Python中的.setlocale()不影响将来的 python 进程。配置环境变量,请参阅如何设置Ubuntu中的所有区域设置


Bash的日期方法似乎以某种方式或另一种方式拾取区域设置。


date call setlocale(LC_ALL, )在开始时,您需要调用 setlocale()至少每个进程一次启用 $ LANG 语言环境而不是 C 语言环境。 p>




setlocale(LC_ALL,'')根据 $ LANG 首先变量,而不是 $ LANGUAGE (它是相关但不同的: GNU gettext搜索路径包含LC_ALL,LC_CTYPE,LANG和LANGUAGE那个订单。



只要设置 LC_TIME category在Ubuntu上):

 >>>导入区域设置
>>>进口时间
>>>> time.strftime('%A')
'星期二'
>>> locale.getlocale(locale.LC_TIME)
('en_US','UTF-8')
>>> locale.setlocale(locale.LC_TIME,'ru_RU.UTF-8')
'ru_RU.UTF-8'
>>> time.strftime('%A')
'Вторник'
>>> locale.getlocale(locale.LC_TIME)
('ru_RU','UTF-8')

如果 setlocale()没有提高 locale.Error:不支持的区域设置,则相应的区域设置类别设置



您还可以获得知道其职位的工作日名称(在同一个python会话中,语言环境更改):

 >>>进口日历
>>>> calendar.day_name [1]
'Вторник'
>>> locale.nl_langinfo(locale.DAY_3)
'Вторник'

一种便携式打印在不修改全局状态的情况下,在特定地区的工作日是使用 babel 模块

 >>> from datetime import date 
>>>来自babel.dates import format_date#$ pip install babel
>>> format_date(date.today(),format ='EEEE',locale ='en')
'星期二'
>>> format_date(date.today(),format ='EEEE',locale ='ru')
'вторник'
>>> format_date(date.today(),format ='EEEE',locale ='nl')
'dinsdag'


So I've been trying to set the Python locale. I want to because I want to use the weekday name in local language (using strftime('%A')). But currently the weekday is printed in English even though I tried setting the locale several ways. I am running this on a RPi2 with Ubuntu Mate.

I have tried the solutions in this topic What is the correct way to set Python's locale?

I tried explicitly setting locale to nl_NL.utf8:

$> locale -a
$> python3
>>> import locale
>>> locale.setlocale(locale.LC_ALL,'nl_NL.utf-8')

I also tried setting an empty string, a suggested in the last answer:

>>> locale.setlocale(locale.LC_ALL, '')

In both cases when I try

>>> locale.getlocale()

I see nl_NL.utf8

But I still get the weekday in English! Haven't been able to find much about this on the internetz except for the above topic and I couldn't get this fixed.


Edit:

I have tried all 3 options. The first one returns a weird result:

~/Documenten$ python3
Python 3.4.3+ (default, Oct 14 2015, 16:03:50) 
[GCC 5.2.1 20151010] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import locale
>>> locale.getlocale(locale.LC_TIME)
(None, None)
>>> locale.setlocale(locale.LC_TIME, 'nl_NL.utf-8')
'nl_NL.utf-8'
>>> locale.getlocale(locale.LC_TIME)
('nl_NL', 'UTF-8')
>>> exit()
jeffrey@jeffrey-desktop:~/Documenten$ python3
Python 3.4.3+ (default, Oct 14 2015, 16:03:50) 
[GCC 5.2.1 20151010] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import locale
>>> locale.getlocale(locale.LC_TIME)
(None, None)

It seems like nothing is saved. Am I wrong in assuming you set your locale once and then the system will remember this? The second option:

~/Documenten$ python3
Python 3.4.3+ (default, Oct 14 2015, 16:03:50) 
[GCC 5.2.1 20151010] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import calendar
>>> calendar.day_name[2]
'Wednesday'
>>> import locale
>>> locale.nl_langinfo(locale.DAY_2)
'Monday'

Even though I am trying to get a Dutch weekday name I still get an English name in return. Besides that this method starts counting from sunday and not monday. Am I doing anything wrong? The third suggestion got me my weekday name, thanks! But I'm still wondering why the first doesn't work? I am using this script on my own device and would like to work with a correct global locale.


Edit2:

yes, calling locale.setlocale() in Python does not affect future python processes. Configure environment variables instead, see How to set all locale settings in Ubuntu.

Ok! So that is why it didn't work. I assumed I had set the locale for any future use in Python, like an environment variable but then only for Python.

I have a bash script creating files and this Python script should delete it after some time (learning to code, didn't know Python when I wrote the Bash script). Bash's "date" method seems to pick up the locale some way or the other. I'll go ahead and see what I can fix for Python. This is what I see when I type $ locale:

LANG=nl_NL.UTF-8
LANGUAGE=nl:en
LC_CTYPE="nl_NL.UTF-8"
LC_NUMERIC="nl_NL.UTF-8"
LC_TIME="nl_NL.UTF-8"
LC_COLLATE="nl_NL.UTF-8"
LC_MONETARY="nl_NL.UTF-8"
LC_MESSAGES="nl_NL.UTF-8"
LC_PAPER="nl_NL.UTF-8"
LC_NAME="nl_NL.UTF-8"
LC_ADDRESS="nl_NL.UTF-8"
LC_TELEPHONE="nl_NL.UTF-8"
LC_MEASUREMENT="nl_NL.UTF-8"
LC_IDENTIFICATION="nl_NL.UTF-8"
LC_ALL=

解决方案

It seems like nothing is saved. Am I wrong in assuming you set your locale once and then the system will remember this

yes, calling locale.setlocale() in Python does not affect future python processes. Configure environment variables instead, see How to set all locale settings in Ubuntu.

Bash's "date" method seems to pick up the locale some way or the other.

date calls setlocale(LC_ALL, "") at the start i.e., you need to call setlocale() at least once per process to enable $LANG locale instead of C locale.


setlocale(LC_ALL, '') sets locale according to $LANG variable first, not $LANGUAGE (it is related but different: "The GNU gettext search path contains 'LC_ALL', 'LC_CTYPE', 'LANG' and 'LANGUAGE', in that order.").

It is enough to set LC_TIME category (on Ubuntu):

>>> import locale
>>> import time
>>> time.strftime('%A')
'Tuesday'
>>> locale.getlocale(locale.LC_TIME)
('en_US', 'UTF-8')
>>> locale.setlocale(locale.LC_TIME, 'ru_RU.UTF-8')
'ru_RU.UTF-8'
>>> time.strftime('%A')
'Вторник'
>>> locale.getlocale(locale.LC_TIME)
('ru_RU', 'UTF-8')

If setlocale() hasn't raised locale.Error: unsupported locale setting then the corresponding locale category is set successfully.

You could also get the weekday name knowing its position (in the same python session where the locale is changed):

>>> import calendar
>>> calendar.day_name[1]
'Вторник'
>>> locale.nl_langinfo(locale.DAY_3)
'Вторник'

A portable way, to print a weekday in a given locale without modifying a global state, is to use babel module:

>>> from datetime import date
>>> from babel.dates import format_date # $ pip install babel
>>> format_date(date.today(), format='EEEE', locale='en')
'Tuesday'
>>> format_date(date.today(), format='EEEE', locale='ru')
'вторник'
>>> format_date(date.today(), format='EEEE', locale='nl')
'dinsdag'

这篇关于设置Python区域设置不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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