Basic Python programming to convert month name to month using a dictionary

I am new to python and only know the most basic level. I have to allow date input in dd / mm / yyyy format and convert it to something like Aug 26, 1986. I am stuck on how to convert month (mm) from number to words. Below is my current code, I hope you can help me. ** Please do not suggest using the calendar function, we have to use a dict to resolve this issue.

Thank (:

#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 

      

+3


source to share


4 answers


Use Python datetime.datetime! Read with help 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

      

+10


source


After you've done the split, you don't need to use an index like day = date [: 2]. Just use say = date [0]. Likewise, no loop 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 

      

+2


source


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

      

At startup:

Please enter the date in the format of dd/mm/year: 1/5/2004
1 May , 2004

      

+2


source


When you split the date string, you only have three elements (0, 1, and 2):

>>> date=date.split('/')
>>> print date
['11', '12', '2012']
  ^     ^     ^
  0     1     2

      

So the date [: 2] will be equal to this:

>>> day=date[:2] # that is, date up to (but not including) position 2
>>> print day
['11', '12']

      

And date[4]

it will not exist, and it will not date[3:5]

.

Also, you need to call the dictionary value like this:

>>> print monthDict[12]
Dec

      

So, to print out the combination of "day, month, year", you would like to do this:

>>> print date[0], monthDict[int(date[1])] + ", " + date[2]
11 Dec, 2012

      

You must use int(date[0])

as your key in monthDict[int(date[0])]

, because you used integers as dictionary keys. But your input (from user) is a string, not integers.

+1


source







All Articles