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