Reputation: 55
I am new to python and only know the most basic level. I am supposed to allow input of a date in the form of dd/mm/yyyy and convert it to something like 26 Aug, 1986. I am stuck as to how to convert my month(mm) from numbers to words. Below is my current code, hope you can help me. ** please do not suggest using calendar function, we are supposed to use dict to solve this question.
Thank you (:
#allow the user to input the date
date=raw_input("Please enter the date in the format of dd/mm/year: ")
#split the strings
date=date.split('/')
#day
day=date[:2]
#create a dictionary for the months
monthDict={1:'Jan', 2:'Feb', 3:'Mar', 4:'Apr', 5:'May', 6:'Jun', 7:'Jul', 8:'Aug', 9:'Sep', 10:'Oct', 11:'Nov', 12:'Dec'}
#month
month=date[3:5]
if month in monthDict:
for key,value in monthDict:
month=value
#year
year=date[4:]
#print the result in the required format
print day, month, "," , year
Upvotes: 4
Views: 37790
Reputation: 42072
Use Python's datetime.datetime! Read using my_date = strptime(the_string, "%d/%m/%Y")
. Print it using my_date.strftime("%d %b, %Y")
.
Visit: http://docs.python.org/2/library/datetime.html#strftime-and-strptime-behavior
Example:
import datetime
input = '23/12/2011'
my_date = datetime.datetime.strptime(input, "%d/%m/%Y")
print my_date.strftime("%d %b, %Y") # 23 Dec, 2011
Upvotes: 12
Reputation: 11591
When you split your date string, you will only have three elements (0, 1, and 2):
>>> date=date.split('/')
>>> print date
['11', '12', '2012']
^ ^ ^
0 1 2
Thus, date[:2] will equal this:
>>> day=date[:2] # that is, date up to (but not including) position 2
>>> print day
['11', '12']
And date[4]
will not exist, and neither will date[3:5]
.
In addition, you need to call your dictionary value like this:
>>> print monthDict[12]
Dec
So to print the day, month, year combination, you would want to do this:
>>> print date[0], monthDict[int(date[1])] + ", " + date[2]
11 Dec, 2012
You have to use int(date[0])
as your key in monthDict[int(date[0])]
because you used integers as your dictionary keys. But your input (from the user) is a string, not integers.
Upvotes: 1
Reputation: 59974
date = raw_input("Please enter the date in the format of dd/mm/year: ")
date = date.split('/')
day = date[0] # date is, for example, [1,2,1998]. A list, because you have use split()
monthDict = {1:'Jan', 2:'Feb', 3:'Mar', 4:'Apr', 5:'May', 6:'Jun',
7:'Jul', 8:'Aug', 9:'Sep', 10:'Oct', 11:'Nov', 12:'Dec'}
month = date[1] # Notice how I have changed this as well
# because the length of date is only 3
month = monthDict[int(month)]
year = date[2] # Also changed this, otherwise it would be an IndexError
print day, month, "," , year
When run:
Please enter the date in the format of dd/mm/year: 1/5/2004
1 May , 2004
Upvotes: 4
Reputation: 4391
After you have done split, you don't need to use index like day=date[:2]. Simply use say = date[0]. Similarly no looping is required to match dictionary values. You can see the code below.
#allow the user to input the date
date=raw_input("Please enter the date in the format of dd/mm/year: ")
#split the strings
date=date.split('/')
#day
day=date[0]
#create a dictionary for the months
monthDict={1:'Jan', 2:'Feb', 3:'Mar', 4:'Apr', 5:'May', 6:'Jun', 7:'Jul', 8:'Aug', 9:'Sep', 10:'Oct', 11:'Nov', 12:'Dec'}
#month
monthIndex= int(date[1])
month = monthDict[monthIndex]
#year
year=date[2]
print day, month, "," , year
Upvotes: 2