Python 日历示例教程
Python 中的日历模块具有日历类,允许基于日期、月份和年份计算各种任务。最重要的是,Python 中的 TextCalendar 和 HTMLCalendar 类允许您编辑日历并根据您的要求使用。
让我们看看我们可以用 Python 日历做什么。
步骤1) 运行代码。
- 代码行 #1:我们从“import calendar”开始,它将导入该模块的所有类。
- 代码行#3:c=calendar.TextCalendar(calendar.SUNDAY) 告诉解释器创建一个文本日历。本月初将是星期日。在 Python 中,您可以设置日历的格式,因为您可以更改月份的开始日期
- 代码行 #4:str=c.formatmonth(2025,1) 我们正在为 2025 年的第 1 个月 - 1 月创建日历
- 第 5 行代码:print str 将打印输出。
让我们快速将值从星期日更改为星期四并检查输出
步骤 2) 您还可以打印 HTML 格式的日历,如果开发人员想对日历的外观和感觉进行任何更改,此功能将很有帮助
步骤 3) 使用 c.itermonthday (2025,4) 循环一个月的天数,它将获取该月的总天数。
- 当您执行代码以获取特定月份的总天数时说“四月”,那么您将在输出中获得 30 天,但您还会在开始时看到一些零,有时在结束。
- 输出中的零表示一周中的某一天在一个重叠的月份中,这意味着它不属于该月份。
- 这些零出现在输出中,因为在您的代码中您提到了日期(星期四),因此当您调用函数“c.itermonthdays”时,它将从星期四开始计算天数,而您的星期四可能不会从日期 1 开始 st 4 月可能是 28 th 或 29 th 3 月,因此当您执行代码时,它将从 28 th 开始计算天数 3 月及之后的任何日子,直到 1 st 四月。这些天数将被计为零,在输出中您会看到这些零,同样适用于月底。
- 因此,除了日期 1-30 之外,前一个月和后一个月的所有日期都将在输出中显示为零。
第 4 步) 您可以从本地系统中获取数据,例如月份或工作日等
- 此处的输出显示我们已从本地系统打印出月份名称。同样,您也可以获取工作日名称,如下所示
- 输出将取决于本地系统,假设如果您的本地系统是其他国家/地区,那么它将根据该国家/地区的本地设置给出输出。在这里我们有几个月,所以不会有什么不同,但如果是一周或一天,肯定会有所不同。
第 5 步) 您可以获取全年特定日期的列表。例如,每周的第一个星期一有一个审计日。您想知道每个月的第一个星期一的日期。您可以使用此代码
- mycal =calendar.monthcalendar(2025, month) 将为该月创建日历
- 将变量 week1 和 week2 设置为日历的第一周和第二周
- 检查第 1 周是否包含星期一,设置审核日
- 将审核日设置为第 2 周的第一个星期一
- 输出显示该月第一个星期一的日期。
- 此 Cal 对象的长度将是一定长度,具体取决于该月的周数。在我们的例子中,这将是一个或两个,因为一周的第一个星期一最常出现在第一周,但如果不是,那么考虑第二周。让我们详细看看为什么我们还要考虑第二周。
- 这里我们使用日历的常量星期一,日历对象为您提供代表星期日、星期一、星期二等的常量。我们以前见过这些。因此,如果在第一周,Monday 常量表示的日期不等于 0,请记住零表示属于另一个月份的日期。所以,在这种情况下,如果它为零,它将是属于上个月的星期一。但如果第一个星期一不等于 0,那意味着我的审计日将在第一周内。否则,如果为 0,则第一个星期一不是该月的第一周,而是第二周。
- 那么,我说好吧,将我的审计日变量设置为第二周所代表的星期一。因此,无论是第一周还是第二周,审核日都会回来。
这是完整的代码
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 格式打印日历
- 从本地系统获取数据,例如月份或工作日
- 获取全年特定日期的列表
Python