如何将第 6 个月转换为 6 月?可能吗?
How can I convert month 6 to June? Is it possible?
问题:如何将第 6 个月转换为 6 月?可能吗?
input:
import calendar
y= int(input('Input the year: '))
m=int(input('Input the month: '))
print("Here's the calendar {},{}".format(m,y))
output:
Input the year: 2021
Input the month: 6
Here's the calendar 6,2021
这可行:
import calendar
y= int(input('Input the year: '))
m=int(input('Input the month: '))
months = {
1: 'January',
2: 'February',
3: 'March',
4: 'April',
5: 'May',
6: 'June',
7: 'July',
8: 'August',
9: 'September',
10: 'October',
11: 'November',
12: 'December'
}
print("Here's the calendar {},{}".format(months[m],y))
import calendar
import datetime
y= int(input('Input the year: '))
m=int(input('Input the month: '))
monthObject = datetime.datetime.strptime(str(m),"%m")
monthName = monthObject.strftime("%B")
print("Here's the calendar {},{}".format(monthName,y))
这就是答案
“%B”表示完整的月份名称,“%b”表示缩写的月份名称
例如
monthName = monthObject.strftime("%b")
使用calendar
和enumerate
,我们可以生成映射:
import calendar
mapping = dict(enumerate(calendar.month_name))
这给出了
>>> mapping
{0: "", 1: "January", 2: "February", 3: "March", 4: "April", 5: "May", 6: "June",
7: "July", 8: "August", 9: "September", 10: "October", 11: "November", 12: "December"}
(方便的是它为第 0 个条目提供空字符串,因此我们可以自然地索引,即 6 月为 6,而不是 5)。
那么你可以使用:
>>> inp = 6
>>> month_name = mapping[inp]
>>> month_name
"June"
问题:如何将第 6 个月转换为 6 月?可能吗?
input:
import calendar
y= int(input('Input the year: '))
m=int(input('Input the month: '))
print("Here's the calendar {},{}".format(m,y))
output:
Input the year: 2021
Input the month: 6
Here's the calendar 6,2021
这可行:
import calendar
y= int(input('Input the year: '))
m=int(input('Input the month: '))
months = {
1: 'January',
2: 'February',
3: 'March',
4: 'April',
5: 'May',
6: 'June',
7: 'July',
8: 'August',
9: 'September',
10: 'October',
11: 'November',
12: 'December'
}
print("Here's the calendar {},{}".format(months[m],y))
import calendar
import datetime
y= int(input('Input the year: '))
m=int(input('Input the month: '))
monthObject = datetime.datetime.strptime(str(m),"%m")
monthName = monthObject.strftime("%B")
print("Here's the calendar {},{}".format(monthName,y))
这就是答案 “%B”表示完整的月份名称,“%b”表示缩写的月份名称 例如
monthName = monthObject.strftime("%b")
使用calendar
和enumerate
,我们可以生成映射:
import calendar
mapping = dict(enumerate(calendar.month_name))
这给出了
>>> mapping
{0: "", 1: "January", 2: "February", 3: "March", 4: "April", 5: "May", 6: "June",
7: "July", 8: "August", 9: "September", 10: "October", 11: "November", 12: "December"}
(方便的是它为第 0 个条目提供空字符串,因此我们可以自然地索引,即 6 月为 6,而不是 5)。
那么你可以使用:
>>> inp = 6
>>> month_name = mapping[inp]
>>> month_name
"June"