Date Parsing Question

Gavin gavin.das1 at gmail.com
Fri Sep 3 17:27:35 EDT 2010


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



More information about the Python-list mailing list