小编Lav*_*nia的帖子

基本Python编程使用字典将月号转换为月份名

我是python的新手,只知道最基本的水平.我应该允许以dd/mm/yyyy的形式输入日期并将其转换为类似于1986年8月26日的日期.我被困在如何将我的月(mm)从数字转换为单词.以下是我目前的代码,希望你能帮助我.**请不要建议使用日历功能,我们应该用dict来解决这个问题.

谢谢 (:

#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 
Run Code Online (Sandbox Code Playgroud)

python python-2.7

4
推荐指数
1
解决办法
2万
查看次数

标签 统计

python ×1

python-2.7 ×1