当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Python calendar monthdays2calendar()用法及代码示例


日历模块允许输出类似于程序的日历,并提供与日历相关的其他有用函数。 “日历”模块中定义的函数和类使用理想化的日历,当前的公历日历在两个方向上都无限期扩展。

Python中的monthdays2calendar()方法用于获取一年中某周的完整列表。

用法: monthdays2calendar(year, month)

参数: 
year: year of the calendar
month: month of the calendar

返回: a list of the weeks in the month.

代码1:


# Python program to demonstrate working of 
# monthdays2calendar() method 
  
# importing calendar module 
import calendar 
  
obj = calendar.Calendar() 
  
year = 2018
mnth = 9
  
# priting with monthdays2calendar 
print(obj.monthdays2calendar(year, mnth))

输出:

[[(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)]]

请注意,输出中的星期是日期数字和工作日数字的seven-tuples的列表。

代码2:迭代周表

# Python program to demonstrate working of 
# monthdays2calendar() method 
  
# importing calendar module 
import calendar 
  
obj = calendar.Calendar() 
  
# iteratign 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)]


相关用法


注:本文由纯净天空筛选整理自Shivam_k大神的英文原创作品 Python calendar module | monthdays2calendar() method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。