Convertir string a date o datetime

Hernan Martinez Foffani hernan en orgmf.com.ar
Vie Mar 17 17:19:17 CET 2006


> Hernan Martinez Foffani escribió:
> >
> >    >>> import datetime
> >    >>> import time
> >    >>>
> >    >>> d = datetime.datetime(2000,12,30)
> >    >>> d.strftime("%x")
> >    '12/30/00'
> >    >>>
> >    >>> s = time.strptime("12/30/00","%x")
> >    >>> datetime.datetime(s[0], s[1], s[2])
> >    datetime.datetime(2000, 12, 30, 0, 0)
> >
>
> Con locale "en" funciona, pero con locale "sp" no:
>
>  >>> import datetime
>  >>> import time
>  >>> import locale
>  >>>
>  >>> locale.setlocale(locale.LC_TIME, "sp")
> 'Spanish_Spain.1252'
>  >>>
>  >>> d = datetime.datetime(2000, 12, 30)
>  >>> d.strftime("%x")
> '30/12/2000'
>  >>>  # ok 30 de diciembre
>
>  >>>
>  >>> s = time.strptime("30/12/2000", "%x")
>
> Traceback (most recent call last):
>    File "<pyshell#85>", line 1, in -toplevel-
>      s = time.strptime("30/12/2000", "%x")
>    File "C:\Python24\lib\_strptime.py", line 293, in strptime
>      raise ValueError("time data did not match format:  data=%s  fmt=%s" %
> ValueError: time data did not match format:  data=30/12/2000  fmt=%x

¿Qué versión de Python estás usando?  Con la 2.4.2 en Windows sí funciona.
No se... quizás tu problema se deba a http://www.python.org/sf/1290505

  Python 2.4.2 (#67, Sep 28 2005, 12:41:11) [MSC v.1310 32 bit (Intel)] on win32
  Type "help", "copyright", "credits" or "license" for more information.
  >>>
  >>> import datetime
  >>> import time
  >>> import locale
  >>>
  >>> locale.setlocale(locale.LC_TIME, "sp")
  'Spanish_Spain.1252'
  >>>
  >>> d = datetime.datetime(2000, 12, 30)
  >>> d.strftime("%x")
  '30/12/2000'
  >>>
  >>> s = time.strptime("30/12/2000", "%x")
  >>>
  >>> s
  (2000, 12, 30, 0, 0, 0, 5, 365, -1)
  >>>
  >>>

-H.
------------ próxima parte ------------
_______________________________________________
Python-es mailing list
Python-es en aditel.org
http://listas.aditel.org/listinfo/python-es


Más información sobre la lista de distribución Python-es