使用字典将月份数字转换为月份名称的基本 Python 编程
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14533709/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Basic Python Programming to convert month number to month name using dictionary
提问by Lavinia
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.
我是python的新手,只知道最基本的水平。我应该允许以 dd/mm/yyyy 的形式输入日期并将其转换为类似 1986 年 8 月 26 日的日期。我对如何将我的月份(毫米)从数字转换为单词感到困惑。下面是我目前的代码,希望你能帮助我。**请不要建议使用日历功能,我们应该使用dict来解决这个问题。
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
回答by π?δα? ?κ??
When you split your date string, you will only have three elements (0, 1, and 2):
拆分日期字符串时,您将只有三个元素(0、1 和 2):
>>> date=date.split('/')
>>> print date
['11', '12', '2012']
^ ^ ^
0 1 2
Thus, date[:2] will equal this:
因此, date[:2] 将等于:
>>> 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].
并且date[4]不会存在,也不会存在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.
您必须使用int(date[0])作为键 inmonthDict[int(date[0])]因为您使用整数作为字典键。但是您的输入(来自用户)是一个字符串,而不是整数。
回答by Sudip Kafle
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.
完成拆分后,您不需要使用诸如 day=date[:2] 之类的索引。只需使用 say = date[0]。类似地,不需要循环来匹配字典值。你可以看到下面的代码。
#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
回答by TerryA
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
回答by Escualo
Use Python's datetime.datetime! Read using my_date = strptime(the_string, "%d/%m/%Y"). Print it using my_date.strftime("%d %b, %Y").
使用 Python 的 datetime.datetime!阅读使用my_date = strptime(the_string, "%d/%m/%Y"). 使用my_date.strftime("%d %b, %Y").
Visit: http://docs.python.org/2/library/datetime.html#strftime-and-strptime-behavior
访问: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

