日历模块允许输出类似于程序的日历,并提供与日历相关的其他有用函数。 “日历”模块中定义的函数和类使用理想化的日历,当前的公历日历在两个方向上都无限期扩展。
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]
相关用法
- Python calendar isleap()用法及代码示例
- Python calendar firstweekday()用法及代码示例
- Python calendar setfirstweekday()用法及代码示例
- Python calendar itermonthdays()用法及代码示例
- Python calendar iterweekdays()用法及代码示例
- Python calendar itermonthdays2()用法及代码示例
- Python calendar monthdatescalendar()用法及代码示例
- Python calendar yeardayscalendar()用法及代码示例
- Python calendar leapdays()用法及代码示例
- Python calendar yeardays2calendar()用法及代码示例
- Python calendar yeardatescalendar()用法及代码示例
- Python calendar monthdays2calendar()用法及代码示例
- Python calendar pryear()用法及代码示例
- Python calendar prmonth()用法及代码示例
- Python calendar formatyear()用法及代码示例
注:本文由纯净天空筛选整理自Shivam_k大神的英文原创作品 Python calendar module | monthdayscalendar() method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。