日历模块允许输出类似于程序的日历,并提供与日历相关的其他有用函数。 “日历”模块中定义的函数和类使用理想化的日历,当前的公历日历在两个方向上都无限期扩展。
calendar.TextCalendar(firstweekday = 0)类可用于生成纯文本日历。 formatyear()方法是TextCalendar实例的方法之一。
使用Python中的formatyear()方法作为multi-line字符串来获取整年的m-column日历。
用法: formatyear(year, width=2, lines=1, c=6, m=3)
参数:
year:日历年
width:[可选]指定宽度日期列
line:[可选]指定每周将使用的行数。
c:[可选]月列之间的间隔数
m:[可选]连续的月数
返回:返回整年的m-column日历。
取决于构造函数中指定的第一个工作日或由setfirstweekday()
方法。
代码1:
# Python program to demonstrate working of formatyear() method
# importing calendar module
import calendar
text_cal = calendar.TextCalendar(firstweekday = 0)
year = 2018
width = 4
# printing formatyear
print(text_cal.formatyear(year, width))
输出:
2018 January February March Mon Tue Wed Thu Fri Sat Sun Mon Tue Wed Thu Fri Sat Sun Mon Tue Wed Thu Fri Sat Sun 1 2 3 4 5 6 7 1 2 3 4 1 2 3 4 8 9 10 11 12 13 14 5 6 7 8 9 10 11 5 6 7 8 9 10 11 15 16 17 18 19 20 21 12 13 14 15 16 17 18 12 13 14 15 16 17 18 22 23 24 25 26 27 28 19 20 21 22 23 24 25 19 20 21 22 23 24 25 29 30 31 26 27 28 26 27 28 29 30 31 April May June Mon Tue Wed Thu Fri Sat Sun Mon Tue Wed Thu Fri Sat Sun Mon Tue Wed Thu Fri Sat Sun 1 1 2 3 4 5 6 1 2 3 2 3 4 5 6 7 8 7 8 9 10 11 12 13 4 5 6 7 8 9 10 9 10 11 12 13 14 15 14 15 16 17 18 19 20 11 12 13 14 15 16 17 16 17 18 19 20 21 22 21 22 23 24 25 26 27 18 19 20 21 22 23 24 23 24 25 26 27 28 29 28 29 30 31 25 26 27 28 29 30 30 July August September Mon Tue Wed Thu Fri Sat Sun Mon Tue Wed Thu Fri Sat Sun Mon Tue Wed Thu Fri Sat Sun 1 1 2 3 4 5 1 2 2 3 4 5 6 7 8 6 7 8 9 10 11 12 3 4 5 6 7 8 9 9 10 11 12 13 14 15 13 14 15 16 17 18 19 10 11 12 13 14 15 16 16 17 18 19 20 21 22 20 21 22 23 24 25 26 17 18 19 20 21 22 23 23 24 25 26 27 28 29 27 28 29 30 31 24 25 26 27 28 29 30 30 31 October November December Mon Tue Wed Thu Fri Sat Sun Mon Tue Wed Thu Fri Sat Sun Mon Tue Wed Thu Fri Sat Sun 1 2 3 4 5 6 7 1 2 3 4 1 2 8 9 10 11 12 13 14 5 6 7 8 9 10 11 3 4 5 6 7 8 9 15 16 17 18 19 20 21 12 13 14 15 16 17 18 10 11 12 13 14 15 16 22 23 24 25 26 27 28 19 20 21 22 23 24 25 17 18 19 20 21 22 23 29 30 31 26 27 28 29 30 24 25 26 27 28 29 30 31
请注意,可以生成日历的最早年份是platform-dependent。
代码2:由于m = 2,所以连续显示的月份数为2。
# Python program to demonstrate working of formatyear() method
# importing calendar module
import calendar
text_cal = calendar.TextCalendar(firstweekday = 0)
# default value of width is 0
# printing formatyear
print(text_cal.formatyear(2018, 5, c = 3, m = 2))
输出:
2018 January February Mon Tue Wed Thu Fri Sat Sun Mon Tue Wed Thu Fri Sat Sun 1 2 3 4 5 6 7 1 2 3 4 8 9 10 11 12 13 14 5 6 7 8 9 10 11 15 16 17 18 19 20 21 12 13 14 15 16 17 18 22 23 24 25 26 27 28 19 20 21 22 23 24 25 29 30 31 26 27 28 March April Mon Tue Wed Thu Fri Sat Sun Mon Tue Wed Thu Fri Sat Sun 1 2 3 4 1 5 6 7 8 9 10 11 2 3 4 5 6 7 8 12 13 14 15 16 17 18 9 10 11 12 13 14 15 19 20 21 22 23 24 25 16 17 18 19 20 21 22 26 27 28 29 30 31 23 24 25 26 27 28 29 30 May June Mon Tue Wed Thu Fri Sat Sun Mon Tue Wed Thu Fri Sat Sun 1 2 3 4 5 6 1 2 3 7 8 9 10 11 12 13 4 5 6 7 8 9 10 14 15 16 17 18 19 20 11 12 13 14 15 16 17 21 22 23 24 25 26 27 18 19 20 21 22 23 24 28 29 30 31 25 26 27 28 29 30 July August Mon Tue Wed Thu Fri Sat Sun Mon Tue Wed Thu Fri Sat Sun 1 1 2 3 4 5 2 3 4 5 6 7 8 6 7 8 9 10 11 12 9 10 11 12 13 14 15 13 14 15 16 17 18 19 16 17 18 19 20 21 22 20 21 22 23 24 25 26 23 24 25 26 27 28 29 27 28 29 30 31 30 31 September October Mon Tue Wed Thu Fri Sat Sun Mon Tue Wed Thu Fri Sat Sun 1 2 1 2 3 4 5 6 7 3 4 5 6 7 8 9 8 9 10 11 12 13 14 10 11 12 13 14 15 16 15 16 17 18 19 20 21 17 18 19 20 21 22 23 22 23 24 25 26 27 28 24 25 26 27 28 29 30 29 30 31 November December Mon Tue Wed Thu Fri Sat Sun Mon Tue Wed Thu Fri Sat Sun 1 2 3 4 1 2 5 6 7 8 9 10 11 3 4 5 6 7 8 9 12 13 14 15 16 17 18 10 11 12 13 14 15 16 19 20 21 22 23 24 25 17 18 19 20 21 22 23 26 27 28 29 30 24 25 26 27 28 29 30 31
相关用法
- Python calendar HTMLCalendar formatyear()用法及代码示例
- Python calendar setfirstweekday()用法及代码示例
- Python calendar firstweekday()用法及代码示例
- Python calendar isleap()用法及代码示例
- 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 monthdayscalendar()用法及代码示例
- Python calendar monthdays2calendar()用法及代码示例
- Python calendar pryear()用法及代码示例
注:本文由纯净天空筛选整理自Shivam_k大神的英文原创作品 Python calendar module | formatyear() method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。