當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。