Figure out month number from month abbrievation
Azolex
cretin at des.alpes.ch
Wed Apr 12 16:55:24 EDT 2006
Bill wrote:
> Hello --
> I'm parsing the output of the finger command, and was wondering
> something...If I'm given a month abbrievation (such as "Jan"), what's
> the best way to figure out the month number?
> I see that there's
> something called "month_abbr" in the calendar module. However, when I
> try to do calendar.month_abbr.index("Jan"), I get "_localized_month
> instance has no attribute 'index'." So it seems that month_abbr isn't
> a regular list. I'm currently doing it this way:
>
> 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.
well, you can define the equivalent of your function with
month_number = list(calendar.month_abbr).index
or else
"! Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec".split().index
More information about the Python-list
mailing list