Figure out month number from month abbrievation

Discussion in 'Python' started by Bill, Apr 12, 2006.

  1. Bill

    Bill Guest

    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.
    Bill, Apr 12, 2006
    #1
    1. Advertising

  2. "Bill" wrote:

    > 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?


    a couple of things, I think.

    .... first, you can use list(seq) to convert any sequence to a list object,
    so you could do

    return list(calendar.month_abbr).index(monthabbr)

    if you prefer to do things in one line.


    .... but it also looks as if the meaning of the word "localized" isn't clear to
    you; if you changed the locale, those names will be translated:

    >>> list(calendar.month_abbr)

    ['', 'jan', 'feb', 'mar', 'apr', 'maj', 'jun', 'jul', 'aug', 'sep', 'okt', 'nov', 'dec']

    which will cause your finger program to break...

    .... and I'm quite sure that you could have written down the abbreviations
    in far less time than it took you to compose that mail ;-)

    MONTHS = ['',
    'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
    'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'
    ]

    month_number = MONTHS.index

    </F>
    Fredrik Lundh, Apr 12, 2006
    #2
    1. Advertising

  3. Bill

    Azolex Guest

    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
    Azolex, Apr 12, 2006
    #3
  4. On 12 Apr 2006 13:20:28 -0700, rumours say that "Bill"
    <> might have written:

    >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?


    Try

    import time
    help(time.strftime)

    and then this *might* work for you:

    month_as_string= "Jan"
    time.strptime(month_as_string, "%b").tm_mon

    "Localization" (as Fredrik also suggested) is the reason for the *might* in
    my previous sentence.
    --
    TZOTZIOY, I speak England very best.
    "Dear Paul,
    please stop spamming us."
    The Corinthians
    Christos Georgiou, Apr 12, 2006
    #4
  5. Bill

    John Salerno Guest

    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?
    John Salerno, Apr 12, 2006
    #5
  6. Bill

    John Machin Guest

    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']
    John Machin, Apr 12, 2006
    #6
  7. Bill

    Azolex Guest

    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?


    you don't have a python shell always at hand for such cases of curiosity ?


    >>> import calendar
    >>> tuple(calendar.month_abbr)

    ('', 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep',
    'Oct', 'Nov', 'Dec')
    Azolex, Apr 12, 2006
    #7
  8. Bill

    John Salerno Guest

    John Machin wrote:

    > 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']


    Ah thanks. I did try it first, but I didn't know to cast it to a list,
    so I was just getting the object.
    John Salerno, Apr 12, 2006
    #8
  9. Bill

    Fulvio Guest

    Alle 04:41, giovedì 13 aprile 2006, Fredrik Lundh ha scritto:
    > but it also looks as if the meaning of the word "localized" isn't clear to
    > you; if you changed the locale, those names will be translated


    Mine behave strangely. Linux localized for Italian, but Python (or its
    calander is in english)

    ??
    F
    Fulvio, Apr 13, 2006
    #9
  10. "Fulvio" wrote:

    > > but it also looks as if the meaning of the word "localized" isn't clear to
    > > you; if you changed the locale, those names will be translated

    >
    > Mine behave strangely. Linux localized for Italian, but Python (or its
    > calander is in english)


    Python defaults to the C locale, which is a minimal english locale. To change this,
    you have to tell the locale module

    >>> import locale
    >>> locale.setlocale(locale.LC_ALL)

    'C'
    >>> import calendar
    >>> list(calendar.month_abbr)

    ['', 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']

    >>> locale.setlocale(locale.LC_ALL, "") # get locale from environment

    'sv_SE'
    >>> list(calendar.month_abbr)

    ['', 'jan', 'feb', 'mar', 'apr', 'maj', 'jun', 'jul', 'aug', 'sep', 'okt', 'nov', 'dec']

    >>> locale.setlocale(locale.LC_ALL, "it_IT") # use explicit locale

    'it_IT'
    >>> list(calendar.month_abbr)

    ['', 'gen', 'feb', 'mar', 'apr', 'mag', 'giu', 'lug', 'ago', 'set', 'ott', 'nov', 'dic']

    The point here is that this is a global setting; once you (or someone using your code)
    change the locale, all locale dependent code changes behaviour.

    >>> import locale, string
    >>> print string.uppercase

    ABCDEFGHIJKLMNOPQRSTUVWXYZ
    >>> locale.setlocale(locale.LC_ALL, "it_IT")

    'it_IT'
    >>> print string.uppercase

    ABCDEFGHIJKLMNOPQRSTUVWXYZ
    ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞ

    </F>
    Fredrik Lundh, Apr 13, 2006
    #10
  11. Bill

    Fulvio Guest

    Alle 20:16, giovedì 13 aprile 2006, Fredrik Lundh ha scritto:
    > >>> locale.setlocale(locale.LC_ALL, "it_IT")

    >
    > 'it_IT'


    Thank you
    Great & kind explanation

    F
    Fulvio, Apr 13, 2006
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Zsolt
    Replies:
    5
    Views:
    605
    John K
    Oct 19, 2004
  2. Michal Maciejczak
    Replies:
    1
    Views:
    1,967
    Matthias Kaeppler
    Apr 23, 2005
  3. VYAS ASHISH M-NTB837

    RE: Convert month name to month number faster

    VYAS ASHISH M-NTB837, Jan 6, 2010, in forum: Python
    Replies:
    0
    Views:
    599
    VYAS ASHISH M-NTB837
    Jan 6, 2010
  4. Steven D'Aprano

    Re: Convert month name to month number faster

    Steven D'Aprano, Jan 6, 2010, in forum: Python
    Replies:
    0
    Views:
    471
    Steven D'Aprano
    Jan 6, 2010
  5. Andrew

    double figure of day and month

    Andrew, May 19, 2004, in forum: ASP General
    Replies:
    2
    Views:
    94
    Andrew
    May 19, 2004
Loading...

Share This Page