How to initialize a table of months.

Steven W. Orr steveo at
Mon Apr 16 03:30:34 CEST 2007

I'm reading a logfile with a timestamp at the begging of each line, e.g.,

Mar 29 08:29:00

I want to call datetime.datetim() whose arg2 is a number between 1-12 so I 
have to convert the month to an integer.
I wrote this, but I have a sneaky suspicion there's a better way to do it.

mons = {'Jan':1, 'Feb':2, 'Mar':3, 'Apr':4, 'May':5, 'Jun':6,
         'Jul':7, 'Aug':8, 'Sep':9, 'Oct':10, 'Nov':11, 'Dec':12 }

def mon2int( mon ):
     global mons
     return mons[mon]

Is there a generator expression or a list comprehension thingy that would 
be *betterer*? (I realize it's probably not that important but I find lots 
of value in learning all the idioms.)


Time flies like the wind. Fruit flies like a banana. Stranger things have  .0.
happened but none stranger than this. Does your driver's license say Organ ..0
Donor?Black holes are where God divided by zero. Listen to me! We are all- 000
individuals! What if this weren't a hypothetical question?
steveo at

More information about the Python-list mailing list