这篇教程Python写得很实用,希望能帮到您。 复习回顾: Python 对于时间日期操作提供了很多方法,我们前面已经学习了2个模块: 
在日常生活工作中,除了每一天时间及日期Python 都提供了具体的模块来进行处理,那么日期的集合要使用什么模块来处理呢? Python 专门为了处理日历提供了calendar日历模块。那么,本期我们来学习一下calender模块相关方法及属性
1. calendar 模块概述calendar 模块提供与日历相关的实用函数,帮助我们得到与日历相关的信息。
1.1calendar 特点 calendar 模块是基于datetime.date.weekday() 对计算每一周的周数 calendar 默认星期一是每一周的第一天,星期天是一周的最后一天 calendar 提供calendar.setfirstweeksday() 来更改指定星期几为一周的第一天
1.2calendar 类结构 - 我们可以查看
Python 内置库-calendar.py 文件 calendar 主要提供三大类:Calendar 、TextCalendar 、HTMLCalendar - 其中,
calendar 是TextCalendar 和HTMLCalendar 的基类 
1.3calendar 使用步骤 calendar 是内置库,直接使用import 导入
2. calendar 相关方法
2.2calendar 模块相关方法 方法 | 说明 | calendar.firstweekday() | 设置每星期的第一天数值 | calendar.isleap(year) | 判断是闰年,则返回Ture | calendar.leapdays(y1,y2) | 计算要y1与y2的闰年数 | calendar.weekday(year,month,day) | 返回某日是星期几 | calendar.weekheader(n) | 星期几的缩写名的头 | calendar.mothrange(year,month) | 计算出指定年份的某月第一天是星期几和天数 | calendar.prmonth(theyear,themonth,w=0,1=0) | 格式化打印指定年的某月的日历 | calendar.month(theyear,themonth,w=0,1=0) | 使用TextCalendar类formation()以多行字符串形式返回月份日历 | calendar.prcal(year,w=1,1=0,c=6,m=3) | 格式化打印出整年的日历 | calendar.calendar(year,w=1,1=0,c=6,m=3) | 以整年3列的日历多行字符串的形式的日历 |
2.3calendar 模块属性 方法 | 说明 | calendar.day_name | 当前语言环境下星期几的数组 | calendar.day_abbr | 当前语言环境下星期几的缩写 | calendar.month_name | 当前语言下一年的月份数组 | calendar.month_abbr | 当前语言下一年的月份缩写 |
2.4calendar 模块提供5个类 类方法 | 说明 | calendar.Calendar(firstweekday=0) | 创建Calendar对象,默认周一为第一天 | calendar.TextCalendar(firstweekday=0) | 生成纯文本日历对象 | calendar.HTMLCalendar(firstweekday=0) | 生成HTML日志对象 | calendar.LocaleTextCalenda(firstweekday=0,locale=None) | 语言环境名称 | calendar.LocaleHTMLCalendar(firstweekday=0,locale=None) | 语言环境名称 |
2.5calendar.Calendar类实例相关方法 方法 | 说明 | cal.itermonthdates(year,month) | 返回一个year年month月的日期的迭代器 | cal.iterweekdats() | 返回为一星期的数字的迭代器 | cal.itermonthdays(year,month) | 返回的日期为当月每一天的日期对应的天数,对于不在当月的日期,会显示0 | cal.itermonthdays2(year,month) | 返回一个由日期和代表星期几的数字组成的元组 | cal.itermonthdays3(year,month) | 返回一个由年月日组成的元组 | cal.itermonthdays4(year,month) | 返回一个由年月日和星期几的数字组成的元组 | cal.monthdatescalendar(year,month) | 返回一个由datetime.date对象组成的年月的周列表 | cal.monthdays2calendar(year,month) | 返回一个由日期数字和周几的数字的二元元组 | cal.monthdayscalendar(year,month) | 返回一个由七个日期数字的组成周列表 | cal.yeardatescalendar(year,width=3) | 返回可以用来格式化的指定年月的数据列表 | cal.yeardays2calendar(year,width=3) | 返回用来模式化的指定年月的数据。在这个月的日期为0,周列表由日期和星期数组成的元组 | cal.yeardayscalendar(year,width=3) | 返回一个周列表是日期数字组成可以用来模式化的指定年月的数据 |
2.6calendar.TextCalendar类实例相关方法 方法 | 说明 | tc.formatmonth(theyear,themonth,w=0,1=0) | 以多行字符串来表示指定年月的日历 | tc.prmonth(theyear,themonth,w=0,1=0) | 格式化打印一个月的日历 | tc.formatyear(theyear,w=0,1=1,c=6,m=3) | 返回一个m列的日历 | tc.pryear(theyear,w=0,1=1,c=6,m=3) | 格式化打印一整年的日历 |
2.7calendar.HTMLCalendar类实例相关方法 方法 | 说明 | htl.formatmonth(theyear,themonth,withyear=True) | 返回一个HTML表格的指定的年月日历 | htl.formatyear(theyear,width=3) | 返回HTML指定年份的日历 | htl.formatyearpage(theyear,width=3,css='calendar.css',encoding=None) | 返回一个完整的HTML页面作为指定的年份日历 | calendar.HTMLCalendar类实例相关属性
2.8calendar.HTMLCalendar类实例相关属性 方法 | 说明 | htl.cssclasses | 星期一到星期天的CSS class 列表 | htl.cssclass_noday | 工作日的CSS类在上个月或下个月发生 | htl.cssclasses_weekday_head | 用于标题行中工作日名称的css列表 | htl.cssclass_month_head | 月份的CSS列表标题 | htl.cssclass_month | 某个月的月历CSS类 | htl.cssclass_year | 某个年的年历CSS类 | htl.cssclasses_year_head | 年历的CSS列表标题 |
3. 实操打印2021年年历 import calendar# 打印2021年历print(calendar.calendar(2021)) 
打印指定某年的月历 print(calendar.month(2021,11))
4、总结我们对calendar模块日历相关的方法的学习,calendar模块主要提供3个主要类Calendar、TextCalendar、HTMLCalendar。我们可以更好地以字符串或者HTML形式打印出指定的日历。 到此这篇关于 Python calendar模块详情的文章就介绍到这了,更多相关 Python calendar模块内容请搜索51zixue.net以前的文章或继续浏览下面的相关文章希望大家以后多多支持51zixue.net! Python 时间操作datetime详情(下) 详解Python的函数与异常 |