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


Python calendar monthdayscalendar()用法及代碼示例


日曆模塊允許輸出類似於程序的日曆,並提供與日曆相關的其他有用函數。 “日曆”模塊中定義的函數和類使用理想化的日曆,當前的公曆日曆在兩個方向上都無限期擴展。

Python中的monthdayscalendar()方法用於獲取一年中一個月中的整周列表。

用法: monthdayscalendar(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 monthdayscalendar() method 
  
# importing calendar module 
import calendar 
  
obj = calendar.Calendar() 
  
year = 2018
mnth = 9
  
# priting with monthdayscalendar 
print(obj.monthdayscalendar(year, mnth))

輸出:

[[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:迭代周表

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


相關用法


注:本文由純淨天空篩選整理自Shivam_k大神的英文原創作品 Python calendar module | monthdayscalendar() method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。