Python and i18n

Peter Otten __peter__ at web.de
Sat Oct 18 14:47:55 EDT 2003


João Alfredo wrote:

> Hi people.
> 
> My LANG environment variable is set to pt_BR.UTF-8 and when raw a date
> command:
> 
> $ date
> Sáb Out 18 11:19:42 BRT 2003
> I get the current date in the apropriate locale.
> 
> Now when i raw the following command in the python interpreter:
> 
> $ python
> Python 2.2.2 (#1, Feb 24 2003, 19:13:11)
> [GCC 3.2.2 20030222 (Red Hat Linux 3.2.2-4)] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
>>>> import time
>>>> time.asctime()
> 'Sat Oct 18 11:25:19 2003'
>>>>
> i get the date in US locale format.
> 
> So, does python support the pt_BR locale?? Am I doing something wrong??

You have to set the locale explicitly:

>>> locale.setlocale(locale.LC_ALL, ("pt", None))
'pt_PT.ISO8859-1'

Even then asctime() will not work as you wish:

>>> time.asctime()
'Sat Oct 18 20:44:29 2003'


But strftime() has it right:

>>> time.strftime("%c")
'S\xe1b 18 Out 2003 20:44:44 CEST'
>>> print time.strftime("%c")
Sáb 18 Out 2003 20:44:54 CEST
>>>

Peter




More information about the Python-list mailing list