Python 日曆模組:yeardayscalendar() 方法


簡介

在 Python 程式設計領域,calendar 模組是一個靈活的工具包,用於管理日期和時間操作。在這個模組中,yeardayscalendar() 方法如同一個特殊的寶石般閃耀。與傳統的日曆函式不同,此方法提供了一種新的視角,將日期組織成周的形式,提供了一種理解時間流逝的替代方式。在本文中,我們將深入探討 yeardayscalendar() 方法,揭示其優點、應用以及它在從每週的角度管理日曆時提供的獨特見解。

探索模組 `yeardayscalendar()` 方法?

Python 的 calendar 模組以其處理日期資訊的能力而聞名,其中包含 yeardayscalendar() 方法。此方法提供了一種不同尋常但巧妙的方式來與日曆互動,重點從月份轉移到周。在一個日曆與按月框架同義的世界中,此方法打開了一個獨特的視角來觀察時間。

yeardayscalendar() 方法的核心是將一年的日曆生成一系列基於周的矩陣。每個矩陣代表一個月的天數,分佈在幾週中。這種替代性安排在處理遵循每週模式的時間相關資料時特別有用。可以將其比作看到由一系列周組成的更大的時間影像。

工作日數字為 0-6,分別代表星期一到星期日。預設情況下,它假設星期一是一週的開始(ISO 8601)。

語法

語法如下:

calendar.yeardayscalendar(year, yeardayslist(optional))

yeardayscalendar() 在內部處理對齊月份日曆、查詢周的開始和結束以及確定月份長度等複雜問題。返回的 3D 矩陣提供了一個方便的資料結構,用於訪問和呈現全年的日曆。模組化結構允許根據需要輕鬆訪問月份、周和日期資料。

引數和返回值

yeardayscalendar() 的引數如下:

  • year − 年份,用四位數字的整數表示

  • yeardayslist − 一年中每一天的工作日數字列表

它返回一個 3D 矩陣,其中包含年度日曆表示。外部列表代表月份,中間列表代表周,內部列表代表工作日。

示例 1

import calendar

year = 2023

cal_obj = calendar.Calendar()

year_layout = cal_obj.yeardayscalendar(year,2)

for quarter in year_layout:
   for month_group in quarter:
      # Print the quarter layout
      print(month_group)

輸出

[[0, 0, 0, 0, 0, 0, 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, 0, 0, 0, 0, 0]]
[[0, 0, 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, 0, 0, 0, 0, 0]]
[[0, 0, 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, 0, 0]]
[[0, 0, 0, 0, 0, 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]]
[[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, 0, 0, 0, 0]]
[[0, 0, 0, 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, 0, 0]]
[[0, 0, 0, 0, 0, 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, 0, 0, 0, 0, 0, 0]]
[[0, 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, 0, 0, 0]]
[[0, 0, 0, 0, 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, 0]]
[[0, 0, 0, 0, 0, 0, 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, 0, 0, 0, 0, 0]]
[[0, 0, 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, 0, 0, 0]]
[[0, 0, 0, 0, 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]]

示例 2

import calendar

year = 2023

cal_obj = calendar.Calendar()

year_layout = cal_obj.yeardayscalendar(year)

for quarter_idx, quarter in enumerate(year_layout, start=1):
   print(f"Quarter {quarter_idx}")
   for month_group in quarter:
      for week in month_group:
         week_str = ""
         for day in week:
            if day == 0:
               week_str += "   "  # Print spaces for days that belong to adjacent months
            else:
               week_str += f"{day:2} "
         print(week_str)
      print()

輸出

Quarter 1
                   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                

       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                

       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       

Quarter 2
                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 

 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             

          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       

Quarter 3
                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                   

    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          

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    

Quarter 4
                   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                

       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          

             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 

結論

Python 的 calendar 模組中的 yeardayscalendar() 方法將給定年份的工作日數字轉換為一個 3D 矩陣,表示全年的日曆。這提供了一個方便的資料結構,用於在各種應用程式中訪問、處理和呈現年度日曆。透過在內部封裝日曆對齊邏輯,yeardayscalendar() 簡化了在 Python 中以程式設計方式處理年度日曆的操作。它是日曆生成和需要結構化年度日曆資料的日期計算的有用工具。

更新於:2023年10月23日

122 次瀏覽

開啟你的職業生涯

完成課程獲得認證

開始學習
廣告
© . All rights reserved.