how to change the time string into number?
Marko Rauhamaa
marko at pacujo.net
Thu Aug 14 01:19:28 EDT 2014
Tim Chase <python.list at tim.thechases.com>:
> Or, if you want a more convoluted way:
>
> >>> import calendar as c
> >>> [i for i, m in enumerate(c.month_abbr) if m == "Aug"].pop()
> 8
Let's not forget the much simpler solutions:
>>> def eight(x): return 8
...
>>> eight("Aug")
8
and:
>>> 8
8
BTW, is this a bug:
>>> import locale
>>> locale.getlocale()
('de_DE', 'UTF-8')
>>> import time
>>> time.strptime("Dez", "%b").tm_mon
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3.2/_strptime.py", line 482, in _strptime_time
tt = _strptime(data_string, format)[0]
File "/usr/lib/python3.2/_strptime.py", line 337, in _strptime
(data_string, format))
ValueError: time data 'Dez' does not match format '%b'
>>> time.strftime("%b", time.localtime(time.time() + 120 * 86400))
'Dec'
>>> time.strftime("%x")
'08/14/14'
After all, "%b" is documented as "Locale’s abbreviated month name."
Anyway, "%b" *should* depend on the locale, so str[pf]time may not be
suitable to deal with email dates, for example.
Marko
More information about the Python-list
mailing list