On Apr 15, 9:30 pm, "Steven W. Orr" <[EMAIL PROTECTED]> wrote: > 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.) > > TIA
Well, I think you want time.strptime. >>> time.strptime("Mar 29 08:29:00", "%b %d %H:%M:%S") (1900, 3, 29, 8, 29, 0, 3, 88, -1) See http://docs.python.org/lib/node85.html However, if strptime did not exist, your dictionary solution is fine -- a tad bit slow, but easy and maintainable, which is worth a lot. -- http://mail.python.org/mailman/listinfo/python-list