Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #76249 > unrolled thread

Re: how to change the time string into number?

Started byTim Chase <python.list@tim.thechases.com>
First post2014-08-13 21:16 -0500
Last post2014-08-14 08:19 +0300
Articles 5 — 5 participants

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: how to change the time string into number? Tim Chase <python.list@tim.thechases.com> - 2014-08-13 21:16 -0500
    Re: how to change the time string into number? YBM <ybmess@nooos.fr.invalid> - 2014-08-14 04:51 +0200
      Re: how to change the time string into number? Ian Kelly <ian.g.kelly@gmail.com> - 2014-08-13 21:07 -0600
      Re: how to change the time string into number? Roy Smith <roy@panix.com> - 2014-08-13 23:14 -0400
    Re: how to change the time string into number? Marko Rauhamaa <marko@pacujo.net> - 2014-08-14 08:19 +0300

#76249 — Re: how to change the time string into number?

FromTim Chase <python.list@tim.thechases.com>
Date2014-08-13 21:16 -0500
SubjectRe: how to change the time string into number?
Message-ID<mailman.12958.1407982655.18130.python-list@python.org>
On 2014-08-13 21:01, Tim Chase wrote:
> On 2014-08-14 09:46, luofeiyu wrote:
> > s="Aug"
> > 
> > how can i change it into 8 with some python time module?
> 
>  >>> import time
>  >>> s = "Aug"
>  >>> time.strptime(s, "%b").tm_mon
>  8
> 
> works for me.

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

-tkc


[toc] | [next] | [standalone]


#76254

FromYBM <ybmess@nooos.fr.invalid>
Date2014-08-14 04:51 +0200
Message-ID<53ec2453$0$2299$426a74cc@news.free.fr>
In reply to#76249
Le 14/08/2014 04:16, Tim Chase a écrit :
> On 2014-08-13 21:01, Tim Chase wrote:
>> On 2014-08-14 09:46, luofeiyu wrote:
>>> s="Aug"
>>>
>>> how can i change it into 8 with some python time module?
>>
>>   >>> import time
>>   >>> s = "Aug"
>>   >>> time.strptime(s, "%b").tm_mon
>>   8
>>
>> works for me.
>
> 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

it's a joke isn't it ?

 >>> import calendar as c
 >>> list(c.month_abbr).index('Aug')
8

BTW, why iterators does not have such an index method ?

 >>> iter(c.month_abbr).index('Aug')
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
AttributeError: 'iterator' object has no attribute 'index'

[toc] | [prev] | [next] | [standalone]


#76255

FromIan Kelly <ian.g.kelly@gmail.com>
Date2014-08-13 21:07 -0600
Message-ID<mailman.12964.1407985691.18130.python-list@python.org>
In reply to#76254

[Multipart message — attachments visible in raw view] — view raw

On Wed, Aug 13, 2014 at 8:51 PM, YBM <ybmess@nooos.fr.invalid> wrote:
> BTW, why iterators does not have such an index method ?

Because iterators don't support indexing. In order to support such a thing,
it would have to exhaust the iterator.

>>> iter(range(5))[3]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'range_iterator' object is not subscriptable

The only methods you can rely upon an arbitrary iterator to have are
__iter__ and __next__.

[toc] | [prev] | [next] | [standalone]


#76257

FromRoy Smith <roy@panix.com>
Date2014-08-13 23:14 -0400
Message-ID<roy-EBE10D.23145813082014@news.panix.com>
In reply to#76254
In article <53ec2453$0$2299$426a74cc@news.free.fr>,
 YBM <ybmess@nooos.fr.invalid> wrote:

> Le 14/08/2014 04:16, Tim Chase a écrit :
> > On 2014-08-13 21:01, Tim Chase wrote:
> >> On 2014-08-14 09:46, luofeiyu wrote:
> >>> s="Aug"
> >>>
> >>> how can i change it into 8 with some python time module?
> >>
> >>   >>> import time
> >>   >>> s = "Aug"
> >>   >>> time.strptime(s, "%b").tm_mon
> >>   8
> >>
> >> works for me.
> >
> > 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
> 
> it's a joke isn't it ?

No, it's a song.

If I could save time in a bottle
The first thing that I'd like to do
Is to make every month be an integer number
And then I could count them with you.

[toc] | [prev] | [next] | [standalone]


#76263

FromMarko Rauhamaa <marko@pacujo.net>
Date2014-08-14 08:19 +0300
Message-ID<874mxfd4wv.fsf@elektro.pacujo.net>
In reply to#76249
Tim Chase <python.list@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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web