文章目錄
- 主要函式
- **Calendar**
- **TextCalendar**
- **HTMLCalendar**
calendar中封裝了一些類似列舉的變數,首先是星期
| 0 | 1 | 2 | 3 | 4 | 5 | 6 |
|---|---|---|---|---|---|---|
| MONDAY | TUESDAY | WEDNESDAY | THURSDAY | FRIDAY | SATURDAY | SUNDAY |
以及兩個月份February、January,
主要函式
calendar中最便利的就是pr函式,可以直接列印出相關的日歷,以及對應的字串輸出函式,
主要有列印某一年的日歷;prmonth列印某個月的日歷;
| 列印函式 | 輸出函式 | 輸出陣列 |
|---|---|---|
prcal | calendar | |
prmonth | month | monthcalendar |
例如
>>> import calendar
>>> calendar.prcal(2021,m=4)
2021
January February March April
Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su
1 2 3 1 2 3 4 5 6 7 1 2 3 4 5 6 7 1 2 3 4
4 5 6 7 8 9 10 8 9 10 11 12 13 14 8 9 10 11 12 13 14 5 6 7 8 9 10 11
11 12 13 14 15 16 17 15 16 17 18 19 20 21 15 16 17 18 19 20 21 12 13 14 15 16 17 18
18 19 20 21 22 23 24 22 23 24 25 26 27 28 22 23 24 25 26 27 28 19 20 21 22 23 24 25
25 26 27 28 29 30 31 29 30 31 26 27 28 29 30
May June July August
Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su
1 2 1 2 3 4 5 6 1 2 3 4 1
3 4 5 6 7 8 9 7 8 9 10 11 12 13 5 6 7 8 9 10 11 2 3 4 5 6 7 8
10 11 12 13 14 15 16 14 15 16 17 18 19 20 12 13 14 15 16 17 18 9 10 11 12 13 14 15
17 18 19 20 21 22 23 21 22 23 24 25 26 27 19 20 21 22 23 24 25 16 17 18 19 20 21 22
24 25 26 27 28 29 30 28 29 30 26 27 28 29 30 31 23 24 25 26 27 28 29
31 30 31
September October November December
Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su
1 2 3 4 5 1 2 3 1 2 3 4 5 6 7 1 2 3 4 5
6 7 8 9 10 11 12 4 5 6 7 8 9 10 8 9 10 11 12 13 14 6 7 8 9 10 11 12
13 14 15 16 17 18 19 11 12 13 14 15 16 17 15 16 17 18 19 20 21 13 14 15 16 17 18 19
20 21 22 23 24 25 26 18 19 20 21 22 23 24 22 23 24 25 26 27 28 20 21 22 23 24 25 26
27 28 29 30 25 26 27 28 29 30 31 29 30 27 28 29 30 31
此外還有一些實用的函式
isleap:判斷是否為閏年weekday:輸入年、月、日,回傳星期monthrange:輸入年、月,回傳這個月份第一天的星期和本月天數,firstweekday:無引數,回傳當前日歷第一天的星期,setfirstweekday:更改每周第一天的星期,默認為0代表周一,weekheader:輸入為字符寬度,輸出每周的縮寫,
在很多函式中,都提供了一些用于格式化的引數,可選引數 w, l, c ,m分別表示日期列數, 周的行數, 和月之間的間隔,
calendar中封裝了一些Canlendar類,主要有:
- Calendar
- TextCalendar
- HTMLCalendar
Calendar
顧名思義,Calendar就是日歷,創建一個新日歷必須宣告每周第一天是周幾,默認為0,代表周一,
Calendar中封裝了許多迭代器,迭代器中包含__next__方法,即可通過next()函式來更改輸出值,例如iterweekdays(),其功能為星期所對應的數字,示例如下
>>> cal = calendar.Calendar(0)
>>> weekday = cal.iterweekdays()
>>> next(weekday)
0
>>> next(weekday)
1
>>> next(weekday)
2
>>> list(weekday)
[3, 4, 5, 6]
此外,Calendar中封裝的迭代器如下,其輸入皆為年和月所對應的數值,回傳皆為y年m月所對應的日期,
| 回傳內容 | |
|---|---|
itermonthdates(y, m) | 日期包含了本月頭尾用于組成完整一周的日期, |
itermonthdays(y, m) | 當月每日對應的天數,不在本月的日期顯示為0 |
itermonthdays2(y, m) | 迭代器的元素為由日期和星期組成的的元組 |
itermonthdays3(y, m) | 迭代器的素為年、月、日組成的元組, |
itermonthdays4(y, m) | 迭代器元素為年、月、日、星期組成的元組, |
此外,還封裝了一些回傳串列的函式
monthdatescalendar(year, month):回傳指定年月的周串列,其元素為datetime.date物件,monthdays2calendar(year, month):回傳指定年月的周串列,每周的元素為七個日期和星期組成的二元元組monthdayscalendar(year, month):回傳一個表示指定年月的周串列,周串列由七個代表日期的數字組成,yeardatescalendar(year, width=3):回傳月份組成的串列,每一行包含了最多 width 個月(默認為3),每個月包含了4到6周,每周包含1–7天,每一天使用 datetime.date 物件,yeardays2calendar(year, width=3):回傳可以用來模式化的指定年月的資料(與 yeardatescalendar() 類似),周串列的元素是由表示日期的數字和表示星期幾的數字組成的元組,不在這個月的日子為0,yeardayscalendar(year, width=3):回傳可以用來模式化的指定年月的資料(與 yeardatescalendar() 類似),周串列的元素是表示日期的數字,不在這個月的日子為0,
TextCalendar
用于生成純文本日歷,創建時同樣需要宣告周的第一天,
該類首先提供了格式化日歷的生成方法,主要包括format和pr兩種格式,前者生成字串,后者直接在命令列列印自動換行的文本,其常用的功能即列印月份和年,如下表所示,
| 直接列印 | 格式化 | 輸入 |
|---|---|---|
pryear | formatyear | 年份 |
prmonth | formatmonth | 年、月 |
prweek | formatweek | 星期對應的二元陣列,字寬 |
HTMLCalendar
用于生成xml日歷,
與TextCalendar相似,HTMLCalendar也提供了formatmonth、formatyerar函式,此外還有直接輸出一個完整HTML頁面的函式formatyearpage,
接下來可看一下TextCalendar和HTMLCalendar的區別:
>>> t.formatmonth(2021,10)
' October 2021\nMo Tu We Th Fr Sa Su\n 1 2 3\n 4 5 6 7 8 9 10\n11 12 13 14 15 16 17\n18 19 20 21 22 23 24\n25 26 27 28 29 30 31\n'
>>> t.prmonth(2021,10)
October 2021
Mo Tu We Th Fr Sa Su
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 31
>>> h = HTMLCalendar(0)
>>> h.formatmonth(2021,10)
'<table border="0" cellpadding="0" cellspacing="0" class="month">\n<tr><th colspan="7" class="month">October 2021</th></tr>\n<tr><th class="mon">Mon</th><th class="tue">Tue</th><th class="wed">Wed</th><th class="thu">Thu</th><th class="fri">Fri</th><th class="sat">Sat</th><th class="sun">Sun</th></tr>\n<tr><td class="noday"> </td><td class="noday"> </td><td class="noday"> </td><td class="noday"> </td><td class="fri">1</td><td class="sat">2</td><td class="sun">3</td></tr>\n<tr><td class="mon">4</td><td class="tue">5</td><td class="wed">6</td><td class="thu">7</td><td class="fri">8</td><td class="sat">9</td><td class="sun">10</td></tr>\n<tr><td class="mon">11</td><td class="tue">12</td><td class="wed">13</td><td class="thu">14</td><td class="fri">15</td><td class="sat">16</td><td class="sun">17</td></tr>\n<tr><td class="mon">18</td><td class="tue">19</td><td class="wed">20</td><td class="thu">21</td><td class="fri">22</td><td class="sat">23</td><td class="sun">24</td></tr>\n<tr><td class="mon">25</td><td class="tue">26</td><td class="wed">27</td><td class="thu">28</td><td class="fri">29</td><td class="sat">30</td><td class="sun">31</td></tr>\n</table>\n'
>>>
關于日期的更多知識,可見于一文擼懂python內置的時間模塊
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/332102.html
標籤:python
