當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Python calendar pryear()用法及代碼示例


日曆模塊允許輸出類似於程序的日曆,並提供與日曆相關的其他有用函數。 “日曆”模塊中定義的函數和類使用理想化的日曆,當前的公曆日曆在兩個方向上都無限期擴展。

calendar.TextCalendar(firstweekday = 0)類可用於生成純文本日曆。 pryear()方法是TextCalendar實例的方法之一。

pryear()Python中的方法用於打印由返回的整年的日曆formatmonth()


用法: pryear(year, width=2, lines=1, c=6, m=3)

參數:
year:日曆年
width:[可選]指定寬度日期列
line:[可選]指定每周將使用的行數。
c:[可選]月列之間的間隔數
m:[可選]連續的月數

返回:返回整年的m-column日曆。

代碼1:

# Python program to demonstrate working of pryear() method 
    
# importing calendar module  
import calendar  
    
text_cal = calendar.TextCalendar(firstweekday = 0)  
    
year = 2018
width = 4
    
# printing pryear 
print(text_cal.pryear(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 pryear() method  
    
# importing calendar module  
import calendar  
    
text_cal = calendar.TextCalendar(firstweekday = 0)  
    
    
# default value of width is 0  
    
# printing pryear 
print(text_cal.pryear(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

None


相關用法


注:本文由純淨天空篩選整理自Shivam_k大神的英文原創作品 Python calendar module | pryear() method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。