[Tutor] date formatter
Kent Johnson
kent37 at tds.net
Fri Aug 8 13:49:06 CEST 2008
On Fri, Aug 8, 2008 at 1:34 AM, Christopher Spears
<cspears2002 at yahoo.com> wrote:
> Ok, here is the working version of my program. Thanks for all of the advice:
>
> #!/usr/bin/python
>
> import time
>
> class date_format(object):
This is a bit of a misnomer, you aren't formatting the date, you are
parsing it.
> def __init__(self, month, day, year):
> month_dict = {("jan","january") : 1,
> ("feb","february") :2,
> ("mar", "march") : 3,
> ("apr", "april") : 4,
> ("may",) : 5,
> ("jun", "june") : 6,
> ("jul", "july") : 7,
> ("aug", "august") : 8,
> ("sep", "september") : 9,
> ("oct", "october"): 10,
> ("nov", "november"): 11,
> ("dec", "december"): 12
> }
>
> try:
> month_number = int(month)
> except ValueError:
> for eachKey in month_dict.keys():
> if month.lower() in eachKey:
> month_number = month_dict[eachKey]
> break
month_dict might as well be a list, you aren't using the dict-ness of it at all.
months = [ (("jan","january") , 1),
... ]
try:
month_number = int(month)
except ValueError:
for names, number in months():
if month.lower() in names:
month_number = number
break
else:
month_number = 0
You might be interested in this project:
http://code-bear.com/code/parsedatetime/
Kent
More information about the Tutor
mailing list