Changing Locale for datetime.strptime conversions

Discussion in 'Python' started by AlienBaby, Jul 6, 2010.

  1. AlienBaby

    AlienBaby Guest

    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
     
    AlienBaby, Jul 6, 2010
    #1
    1. Advertising

  2. AlienBaby

    AlienBaby Guest

    On 6 July, 10:55, AlienBaby <> 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.
     
    AlienBaby, Jul 6, 2010
    #2
    1. Advertising

  3. AlienBaby

    AlienBaby Guest

    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?
     
    AlienBaby, Jul 6, 2010
    #3
  4. On Tue, 6 Jul 2010 03:21:21 -0700 (PDT)
    AlienBaby <> 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
     
    Antoine Pitrou, Jul 6, 2010
    #4
  5. AlienBaby

    Guest

    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
     
    , Jul 6, 2010
    #5
  6. On Tue, 06 Jul 2010 11:54:46 -0400
    wrote:
    > 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.


    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.
     
    Antoine Pitrou, Jul 6, 2010
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    10
    Views:
    724
    Jasen Betts
    Aug 5, 2005
  2. kongkolvyu
    Replies:
    4
    Views:
    496
    Old Wolf
    Jan 4, 2006
  3. Replies:
    4
    Views:
    1,053
  4. Andy B
    Replies:
    2
    Views:
    2,247
    Martin Honnen
    Sep 25, 2008
  5. Tuomas Vesterinen

    Problems with datetime.datetime.strptime

    Tuomas Vesterinen, May 9, 2009, in forum: Python
    Replies:
    0
    Views:
    918
    Tuomas Vesterinen
    May 9, 2009
Loading...

Share This Page