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. 10 7293
"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>
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
On 12 Apr 2006 13:20:28 -0700, rumours say that "Bill"
<fo**********@ftml.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.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
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?
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 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')
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.
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" 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>
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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...
|
by: developer |
last post by:
I want to substract a number of month from a specific date.
someone have a easy solution ?
Thanks
|
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
|
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...
|
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...
|
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...
|
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...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
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,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
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...
|
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...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
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...
| |