Figure out month number from month abbrievation

John Machin sjmachin at lexicon.net
Wed Apr 12 17:10:58 EDT 2006


On 13/04/2006 7:02 AM, John Salerno wrote:
> Bill wrote:
> 
>> def month_number(monthabbr):
>>     """Return the month number for monthabbr; e.g. "Jan" -> 1."""
>>     for index, day in enumerate(calendar.month_abbr):
>>         if day == monthabbr:
>>             return index
>>
>> which works well enough but isn't very clever.  I'm pretty new to
>> Python; what am I missing here?
>> Thanks -- Bill.
> 
> I'm curious, does that really work, or is there a problem with the first 
> index being 0? Or is that avoided somehow?

Yes, No, Yes.

You can answer such questions yourself very easily, e.g. in this case:

 >>> import calendar
 >>> calendar.month_abbr
<calendar._localized_month instance at 0x00AE84E0>
 >>> list(calendar.month_abbr)
['', 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 
'Oct', 'Nov', 'Dec']



More information about the Python-list mailing list