Changing Locale for datetime.strptime conversions

A

AlienBaby

Hi,

I'm using datetime.strptime(string,format) to convert dates parsed
from a file into datetime objects.

However, the files come from various places around the world, and
strptime fails when non-english month names are used.

strptime says it converts month names using the current locales
version of the name. I've looked into the locale module but can't see
how I would setup.change a locales date/time representations, I can
only see categories related to decimal number / currency
representations.

Can anyone show how I could change the locale such that strptime could
parse a date string that used say, German month names?

Thankyou
 
A

AlienBaby

Hi,

I'm using datetime.strptime(string,format) to convert dates parsed
from a file into datetime objects.

However, the files come from various places around the world, and
strptime fails when non-english month names are used.

strptime says it converts month names using the current locales
version of the name.  I've looked into the locale module but can't see
how I would setup.change a locales date/time representations, I can
only see categories related to decimal number / currency
representations.

Can anyone show how I could change the locale such that strptime could
parse a date string that used say, German month names?

Thankyou

I just solved this I believe. I didnt spot LC_ALL or LC_TIME
previously.
 
A

AlienBaby

I'm still having a bit of trouble, for example trying to set the
locale to Denmark


locale.setlocale(locale.LC_ALL, locale.normalize('da_DK'))

returns with

locale.setlocale(locale.LC_ALL, locale.normalize('da_DK'))
File "C:\Python26\lib\locale.py", line 494, in setlocale
return _setlocale(category, locale)
locale.Error: unsupported locale setting


Though, from the docs I understand normalize should return a local
formatted for use with setlocale?
 
A

Antoine Pitrou

I'm still having a bit of trouble, for example trying to set the
locale to Denmark


locale.setlocale(locale.LC_ALL, locale.normalize('da_DK'))

returns with

locale.setlocale(locale.LC_ALL, locale.normalize('da_DK'))
File "C:\Python26\lib\locale.py", line 494, in setlocale
return _setlocale(category, locale)
locale.Error: unsupported locale setting


Though, from the docs I understand normalize should return a local
formatted for use with setlocale?

I think normalize works ok, but setlocale then fails (*). You can only
use a locale if it's installed on the computer. That, and other issues
(such as the fact that the locale setting is process-wide and can
interfere with other parts of your program, or third-party libraries;
or the fact that a given locale can have differences depending on the
vendor) make the locale mechanism very fragile and annoying.

If you want to do this seriously, I suggest you instead take a look at
third-party libraries such as Babel:
http://babel.edgewall.org/


(*):
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib64/python2.6/locale.py", line 513, in setlocale
return _setlocale(category, locale)
locale.Error: unsupported locale setting
 
P

python

Antoine,
If you want to do this seriously, I suggest you instead take a look at third-party libraries such as Babel: http://babel.edgewall.org/

Not the OP, but has Babel implemented parsing support? Last time I
looked, Babel did a great job with locale specific formatting, but
locale specific formatting was still incomplete.

Malcolm
 
A

Antoine Pitrou

On Tue, 06 Jul 2010 11:54:46 -0400
Antoine,


Not the OP, but has Babel implemented parsing support? Last time I
looked, Babel did a great job with locale specific formatting, but
locale specific formatting was still incomplete.

No idea, but if you just want to recognize month names, you can produce
all the month names in the desired natural language and then recognize
them yourself (using e.g. a regexp). Probably imperfect, but probably
sufficient in many cases too.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,816
Latest member
nipsseyhussle

Latest Threads

Top