Figure out month number from month abbrievation

B

Bill

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.
 
F

Fredrik Lundh

Bill said:
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:
['', '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>
 
A

Azolex

Bill said:
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
 
C

Christos Georgiou

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.
 
J

John Salerno

Bill said:
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?
 
J

John Machin

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:
['', 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep',
'Oct', 'Nov', 'Dec']
 
A

Azolex

John said:
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 ?

('', 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep',
'Oct', 'Nov', 'Dec')
 
J

John Salerno

John said:
You can answer such questions yourself very easily, e.g. in this case:
['', '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.
 
F

Fulvio

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
 
F

Fredrik Lundh

Fulvio said:
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
['', 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
['', 'jan', 'feb', 'mar', 'apr', 'maj', 'jun', 'jul', 'aug', 'sep', 'okt', 'nov', 'dec']
['', '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.
ABCDEFGHIJKLMNOPQRSTUVWXYZ
ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞ

</F>
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top