Python CALENDAR 教程及示例
Python 的 calendar 模块包含 calendar 类,该类允许基于日期、月份和年份进行各种任务的计算。此外,Python 中的 TextCalendar 和 HTMLCalendar 类允许您编辑日历并按需使用。
让我们看看我们可以用 Python Calendar 做什么。
第一步) 运行代码。
- 代码行 # 1:我们以“import calendar”开始,这将导入此模块的所有类。
- 代码行 # 3:c= calendar.TextCalendar(calendar.SUNDAY) 告诉解释器创建一个文本日历。月份的开始将是星期日。在 Python 中,您可以像这样格式化日历:您可以更改月份的开始日期
- 代码行 # 4:str= c.formatmonth(2025,1) 我们正在为 2025 年 1 月创建日历
- 代码行 # 5:print str 将打印输出。
让我们快速将 Sunday 改为 Thursday 并检查输出
第二步) 您还可以以 HTML 格式打印日历,此功能对于想要更改日历外观和感觉的开发人员非常有用
第三步) 使用 c.itermonthday (2025,4) 循环遍历月份中的日期,它将获取该月的总天数。
- 当您执行代码以获取特定月份(例如“四月”)的总天数时,您会在输出中看到 30 天,但您也会在其中一些零以及有时在开始和结束时看到这些零。
- 输出中的零意味着该星期几属于重叠月份,这意味着它不属于该月份。
- 这些零出现在输出中是因为,在您的代码中,您已经提到了星期几(星期四),所以当您调用“c.itermonthdays”函数时,它将从星期四开始计数天数,而您的星期四可能不是从 4 月 1 日开始的,它可能是 3 月 28 日或 29 日,所以当您执行代码时,它将从 3 月 28 日开始计数天数,以及之后直到 4 月 1 日的所有天数。这些天将被计为零,在输出中您将看到这些零,同样也适用于月份的结尾。
- 因此,除了 1-30 日之外,来自上个月和下个月的所有日期都将作为零出现在输出中。
第四步) 您可以从本地系统获取数据,例如月份或星期几等
- 这里的输出显示我们打印了本地系统的月份名称。同样,您也可以获取星期几的名称,如下所示
-
输出将取决于本地系统,假设您的本地系统是其他国家,那么它将根据该国家的本地设置给出输出。这里我们有月份,所以不会有区别,但如果是星期或日期,肯定会有区别。
第五步) 您可以获取一年中特定日期的列表。例如,每周的第一个星期一是审计日。您想知道每个月的第一个星期一的日期。您可以使用此代码
- mycal = calendar.monthcalendar(2025, month) 将为该月创建日历
- 将变量 week1 和 week2 设置为日历的第一周和第二周
- 检查 Week 1 是否包含星期一,设置审计日
- 否则,将审计日设置为第二周的第一个星期一
- 输出显示了该月第一个星期一的日期。
- 此 Cal 对象的长度将是某个长度,取决于月份中有多少周。在我们的例子中,它将是一周或两周,因为第一周的第一个星期一通常会在第一周,但如果不是,则考虑第二周。让我们详细看看为什么我们也考虑第二周。
- 这里我们使用的是日历的常量 Monday,日历对象为您提供了代表 Sunday、Monday、Tuesday 等的常量。我们之前已经见过这些了。所以,如果第一周中代表 Monday 常量的日期不等于 0,请记住零表示属于另一个月份的天数。所以,在这种情况下,如果它是零,那么它将是属于上个月的星期一。但如果第一个星期一不等于零,这意味着我的审计日将在第一周内。否则,如果为零,则第一个星期一不在该月的第一周,它将在第二周。
- 所以,然后我说,好吧,将我的 audit_day 变量设置为第二周代表的星期一。所以,audit_day 将返回第一个或第二周的日期。
这是完整的代码
Python 2 示例
import calendar # Create a plain text calendar c = calendar.TextCalendar(calendar.THURSDAY) str = c.formatmonth(2025, 1, 0, 0) print str # Create an HTML formatted calendar hc = calendar.HTMLCalendar(calendar.THURSDAY) str = hc.formatmonth(2025, 1) print str # loop over the days of a month # zeroes indicate that the day of the week is in a next month or overlapping month for i in c.itermonthdays(2025, 4): print i # The calendar can give info based on local such a names of days and months (full and abbreviated forms) for name in calendar.month_name: print name for day in calendar.day_name: print day # calculate days based on a rule: For instance an audit day on the second Monday of every month # Figure out what days that would be for each month, we can use the script as shown here for month in range(1, 13): # It retrieves a list of weeks that represent the month mycal = calendar.monthcalendar(2025, month) # The first MONDAY has to be within the first two weeks week1 = mycal[0] week2 = mycal[1] if week1[calendar.MONDAY] != 0: auditday = week1[calendar.MONDAY] else: # if the first MONDAY isn't in the first week, it must be in the second week auditday = week2[calendar.MONDAY] print "%10s %2d" % (calendar.month_name[month], auditday)
Python 3 示例
import calendar # Create a plain text calendar c = calendar.TextCalendar(calendar.THURSDAY) str = c.formatmonth(2025, 1, 0, 0) print(str) # Create an HTML formatted calendar hc = calendar.HTMLCalendar(calendar.THURSDAY) str = hc.formatmonth(2025, 1) print(str) # loop over the days of a month # zeroes indicate that the day of the week is in a next month or overlapping month for i in c.itermonthdays(2025, 4): print(i) # The calendar can give info based on local such a names of days and months (full and abbreviated forms) for name in calendar.month_name: print(name) for day in calendar.day_name: print(day) # calculate days based on a rule: For instance an audit day on the second Monday of every month # Figure out what days that would be for each month, we can use the script as shown here for month in range(1, 13): # It retrieves a list of weeks that represent the month mycal = calendar.monthcalendar(2025, month) # The first MONDAY has to be within the first two weeks week1 = mycal[0] week2 = mycal[1] if week1[calendar.MONDAY] != 0: auditday = week1[calendar.MONDAY] else: # if the first MONDAY isn't in the first week, it must be in the second week auditday = week2[calendar.MONDAY] print("%10s %2d" % (calendar.month_name[month], auditday))
摘要
- 在 Python 中,您可以按照您想要的方式格式化日历,因为您可以更改月份的开始日期
- 以 HTML 格式打印日历
- 从本地系统获取数据,例如月份或星期几
- 获取一年中特定日期的列表