Date Parsing Question

Discussion in 'Python' started by Gavin, Sep 3, 2010.

  1. Gavin

    Gavin Guest

    Hi,

    I'm using the python-dateutil package : http://labix.org/python-dateutil
    to parse a set of randomly formatted strings into dates. Because the
    formats are varied, I can't use time.strptime() because I don't know
    what the format is upfront.

    python-dateutil seems to work very well if everything is in English,
    however, it does not seem to work for other languages and the
    documentation does not seem to have any information about locale
    support.

    Here's an example showing my problem:

    Python 2.6.5 (r265:79096, Mar 19 2010, 21:48:26) [MSC v.1500 32 bit
    (Intel)] on win32
    Type "help", "copyright", "credits" or "license" for more information.
    >>> from dateutil.parser import *
    >>> import datetime
    >>> import time
    >>> date_string1 = time.strftime("%Y-%B-%d %H:%M:%S",(2010,10,3,1,1,1,1,1,1))
    >>> print date_string1

    2010-October-03 01:01:01
    >>> parse(date_string1)

    datetime.datetime(2010, 10, 3, 1, 1, 1)

    everything is ok so far, now retry with a date in german:

    >>> import locale
    >>> locale.setlocale(locale.LC_ALL, "german")

    'German_Germany.1252'
    >>> locale.getlocale()

    ('de_DE', 'cp1252')

    >>> date_string1 = time.strftime("%Y-%B-%d %H:%M:%S",(2010,10,3,1,1,1,1,1,1))
    >>> print date_string1

    2010-Oktober-03 01:01:01
    >>> parse(date_string1)

    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    NameError: name 'date_string' is not defined
    >>> parse(date_string1)

    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    File "c:\python26\lib\site-packages\python_dateutil-1.5-py2.6.egg
    \dateutil\parser.py", line 697, in parse
    return DEFAULTPARSER.parse(timestr, **kwargs)
    File "c:\python26\lib\site-packages\python_dateutil-1.5-py2.6.egg
    \dateutil\parser.py", line 303, in parse
    raise ValueError, "unknown string format"
    ValueError: unknown string format


    Am I out of luck with this package? Just wondering if anyone has used
    this to work with non-english dates. I'm also open to other ideas to
    handle this.

    Appreciate the assistance,

    Gavin
    Gavin, Sep 3, 2010
    #1
    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. Matt
    Replies:
    1
    Views:
    564
    Matthew Speed
    Nov 8, 2003
  2. .Net Sports
    Replies:
    1
    Views:
    2,639
    intrader
    Jun 25, 2005
  3. Peter Grison

    Date, date date date....

    Peter Grison, May 28, 2004, in forum: Java
    Replies:
    10
    Views:
    3,180
    Michael Borgwardt
    May 30, 2004
  4. Matt
    Replies:
    2
    Views:
    491
    Pete Becker
    Nov 8, 2003
  5. Matt
    Replies:
    3
    Views:
    690
    Richard Heathfield
    Nov 8, 2003
Loading...

Share This Page