Python日历模块 | monthdayscalendar() 方法
日历模块允许像程序一样输出日历,并提供与日历相关的附加有用功能。 Calendar 模块中定义的函数和类使用理想化的日历,当前的公历在两个方向上无限扩展。
Python中的monthdayscalendar()方法用于获取一年中月份的周列表作为整周。
Syntax: monthdayscalendar(year, month)
Parameter:
year: year of the calendar
month: month of the calendar
Returns: a list of the weeks in the month.
代码#1:
Python3
# Python program to demonstrate working
# of monthdayscalendar() method
# importing calendar module
import calendar
obj = calendar.Calendar()
year = 2018
month = 9
# printing with monthdayscalendar
print(obj.monthdayscalendar(year, month))
Python3
# Python program to demonstrate working
# of monthdayscalendar() method
# importing calendar module
import calendar
obj = calendar.Calendar()
# iterating with monthdayscalendar
for day in obj.monthdayscalendar(2018, 9):
print(day)
输出:
[[0, 0, 0, 0, 0, 1, 2], [3, 4, 5, 6, 7, 8, 9], [10, 11, 12, 13, 14, 15, 16], [17, 18, 19, 20, 21, 22, 23], [24, 25, 26, 27, 28, 29, 30]]
请注意,输出中的周数是 7 天数字的列表。代码 #2:迭代周列表
Python3
# Python program to demonstrate working
# of monthdayscalendar() method
# importing calendar module
import calendar
obj = calendar.Calendar()
# iterating with monthdayscalendar
for day in obj.monthdayscalendar(2018, 9):
print(day)
输出:
[0, 0, 0, 0, 0, 1, 2]
[3, 4, 5, 6, 7, 8, 9]
[10, 11, 12, 13, 14, 15, 16]
[17, 18, 19, 20, 21, 22, 23]
[24, 25, 26, 27, 28, 29, 30]