473,548 Members | 2,578 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Figure out month number from month abbrievation

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_mon th
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(mo nthabbr):
"""Return the month number for monthabbr; e.g. "Jan" -> 1."""
for index, day in enumerate(calen dar.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.

Apr 12 '06 #1
10 7312
"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_mon th
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(mo nthabbr):
"""Return the month number for monthabbr; e.g. "Jan" -> 1."""
for index, day in enumerate(calen dar.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.m onth_abbr).inde x(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.m onth_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>

Apr 12 '06 #2
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_mon th
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(mo nthabbr):
"""Return the month number for monthabbr; e.g. "Jan" -> 1."""
for index, day in enumerate(calen dar.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.m onth_abbr).inde x

or else

"! Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec".split().in dex
Apr 12 '06 #3
On 12 Apr 2006 13:20:28 -0700, rumours say that "Bill"
<fo**********@f tml.net> 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.strft ime)

and then this *might* work for you:

month_as_string = "Jan"
time.strptime(m onth_as_string, "%b").tm_mo n

"Localizati on" (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
Apr 12 '06 #4
Bill wrote:
def month_number(mo nthabbr):
"""Return the month number for monthabbr; e.g. "Jan" -> 1."""
for index, day in enumerate(calen dar.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?
Apr 12 '06 #5
On 13/04/2006 7:02 AM, John Salerno wrote:
Bill wrote:
def month_number(mo nthabbr):
"""Return the month number for monthabbr; e.g. "Jan" -> 1."""
for index, day in enumerate(calen dar.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._loca lized_month instance at 0x00AE84E0> list(calendar.m onth_abbr)

['', 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep',
'Oct', 'Nov', 'Dec']
Apr 12 '06 #6
John Salerno wrote:
Bill wrote:
def month_number(mo nthabbr):
"""Return the month number for monthabbr; e.g. "Jan" -> 1."""
for index, day in enumerate(calen dar.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')
Apr 12 '06 #7
John Machin wrote:
You can answer such questions yourself very easily, e.g. in this case:
>>> import calendar
>>> calendar.month_ abbr <calendar._loca lized_month instance at 0x00AE84E0> >>> list(calendar.m onth_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.
Apr 12 '06 #8
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
Apr 13 '06 #9
"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.setlocal e(locale.LC_ALL ) 'C' import calendar
list(calendar.m onth_abbr) ['', 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
locale.setlocal e(locale.LC_ALL , "") # get locale from environment 'sv_SE' list(calendar.m onth_abbr) ['', 'jan', 'feb', 'mar', 'apr', 'maj', 'jun', 'jul', 'aug', 'sep', 'okt', 'nov', 'dec']
locale.setlocal e(locale.LC_ALL , "it_IT") # use explicit locale 'it_IT' list(calendar.m onth_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.uppercas e ABCDEFGHIJKLMNO PQRSTUVWXYZ locale.setlocal e(locale.LC_ALL , "it_IT") 'it_IT' print string.uppercas e

ABCDEFGHIJKLMNO PQRSTUVWXYZ


</F>

Apr 13 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2986
by: Sugapablo | last post by:
My brain is frozen on a convenient way to figure out if today is the 1st, 2nd, 3rd, 4th, or last Thursday of the month. Basically I need something that will figure this out for any given day for any given month. date() has everything BUT a 0-5 return for this. -- Sugapablo - russpghREMOVE@stargate.net http://www.sugapablo.com | ICQ:...
1
5145
by: Finlay | last post by:
Hi Group I am designing a report that should show a meter reading for each month and the previous meter reading for the previous month. The months are text stored in a field tMonth. The current month is easy to pull out of the data base but I don't know how to get the previous months meter reading. The previous reading is subtracted...
6
17903
by: Tony Miller | last post by:
All I have an aggregate query using the function Month & Year on a datereceived field ie: TheYear: Year() TheMonth: Month() These are the group by fields to give me a Count on another field by year & month When I try to place a date filter 'Between x And y ' on an expression field
7
16226
by: developer | last post by:
I want to substract a number of month from a specific date. someone have a easy solution ? Thanks
4
63131
by: Ronald Celis | last post by:
Hi, is there anyway to get the Number of days in a given month and Year in C# thanks Ronald Celis
6
77805
by: Ashish Sheth | last post by:
Hi All, In C#, How can I get the difference between two dates in number of months? I tried to use the Substract method of the DateTime class and it is giving me the difference in TimeSpan,From which I can get the duration in days, hours and so.. but how can I get the difference in months? Please reply ASAP. it's urgent. -- regards,...
6
8969
by: Ante Perkovic | last post by:
Hi, How to declare datetime object and set it to my birthday, first or last day of this month or any other date. I can't find any examples in VS.NET help! BTW, what is the difference between date and datetime classes? Please, help
5
5491
by: Seb | last post by:
I want to count activity in a given month. I'm trying to do so with the linq code below however it reports: Error 1 'a' is inaccessible due to its protection level var ActivityByMonths = from a in db.ActivityDatas where a.BeginDateTime >= From && a.BeginDateTime <= To where a.SubActivityId == sat group a by a.BeginDateTime.Month into mg...
12
23177
by: Be Borth | last post by:
I saw previous solutions to convert a month number (1) to a month name (January). I have a database with 200+ dates. In a query, I use the "Part" function DatePart("m",), to extract the month (number) and all month extracts worked correctly (they returned the correct numerical month). However, when I attempted adding a field in the query to...
0
7512
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main...
0
7438
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7951
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7466
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7803
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5362
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3495
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1051
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
751
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.