Prev: markmin 0.1
Next: Python GUI for C program [was: ]
From: AlienBaby on 6 Jul 2010 05:55 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
From: AlienBaby on 6 Jul 2010 06:03 On 6 July, 10:55, AlienBaby <matt.j.war...(a)gmail.com> wrote: > 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.
From: AlienBaby on 6 Jul 2010 06:21 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?
From: Antoine Pitrou on 6 Jul 2010 06:38 On Tue, 6 Jul 2010 03:21:21 -0700 (PDT) AlienBaby <matt.j.warren(a)gmail.com> wrote: > 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/ (*): >>> import locale >>> locale.normalize('da_DK') 'da_DK.ISO8859-1' >>> locale.setlocale(locale.LC_ALL, locale.normalize('da_DK')) 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
From: python on 6 Jul 2010 11:54
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 |