Python 數據庫模塊簡介
每當我們開始處理需要實時數據的腳本時,我們都必須使用 dateutil 模塊,以便在特定時間檢索或調度數據,或者輸入帶有檢索時間戳的數據。
考慮到我們必須對檢索到的數據進行的大量更改,使用大量腳本並嘗試在默認日期時間模塊的幫助下操作日期和時間格式可能是一項令人生畏的活動。
幸運的是,開發人員創建了 dateutil 模塊來提供使生活更輕鬆的功能。
在下面的教程中,我們將深入了解 Python dateutil 模塊。Python 模塊是由某些定義和語句組成的文件。我們可以使用這些模塊將一個大程序分解成小程序段,然後將它們導入程序中以發揮它們的功能。當我們必須執行與特定日期和時間相關的工作時,Python 編程語言中的 dateutil 模塊是這些預定義模塊之一。
所以,讓我們開始吧。
了解 Python dateutil
模塊
Python 編程語言中有一個核心模塊,稱為 datetime 模塊。我們使用這個模塊從簡單的方法到複雜的方法來處理日期和時間。儘管這個模塊對於特定數量的實例來說是足夠的,Python 日期時間模塊為日期時間模塊提供了特彆強大的擴展。
Python 數據庫模塊的主要特徵
Python 中 dateutil 模塊的主要特點如下:
- dateutil 模塊支持任何字符串格式的日期解析。
- 本模塊提供內部最新的世界時區詳細信息。
- 該模塊有助於計算相對增量
- 這個模塊還有助於根據非常靈活的重現規則計算日期。
如何在 Python 中安裝 dateutil 模塊?
在我們開始使用 dateutil 模塊之前,主要目標是在系統上安裝該模塊。那麼,讓我們開始安裝程序。
我們將使用 pip 安裝程序,通過在終端中鍵入以下命令來安裝所需的模塊:
語法:
$ pip install python-dateutil
驗證安裝
為了檢查模塊是否已經正確安裝在系統中,我們可以嘗試導入模塊並執行程序。
安裝完成後,創建一個新的 Python 文件,並在其中鍵入以下語法。
示例:
# importing the required module
import dateutil
現在,保存該文件並在命令提示符下使用以下命令運行該文件。
語法:
$ python <file-name>.py
如果程序運行時沒有出現任何導入錯誤,則模塊安裝正確。否則,建議重新安裝該模塊,並參考其官方文檔。
使用 Python dateutil
模塊
現在,讓我們繼續使用 Python 編程語言中的 dateutil 模塊。
dateutil 模塊及其子類
日期模塊分為幾個子類,如下所示:
- 復活節,
- 解析器,
- 相對而言,
- 統治者!統治者
- tz、
以及更多。
這個模塊不包含這麼多子類;然而,在本教程中,我們將只探索其中一些函數。
導入必要的模塊
既然我們已經正確安裝了模塊,現在是時候將方法付諸行動並檢索輸出了。
為了開始使用 Python dateutil 模塊和一些模塊,我們只需要先導入它們。
以下語法顯示了所需模塊的導入。
示例:
# importing methods from the datetime module as a base.
import datetime
# importing several methods from the dateutil subclasses.
from dateutil.relativedelta import *
from dateutil.easter import *
from dateutil.parser import *
from dateutil.rrule import *
說明:
在上面的代碼片段中,我們已經導入了 datetime 模塊來導入方法。然後我們從 dateutil 子類中導入了一些方法。
日期時間功能
在我們進入 dateutil 模塊的編碼部分之前,我們必須了解該模塊依賴於 datetime 模塊。
Python dateutil 模塊與日期時間對象一起工作,這意味着我們必須在開始使用日期時間對象之前創建它們。
因此,有必要導入日期時間模塊。現在,讓我們開始使用 dateutil 模塊中的一些方法。
Datetime 和 relativedelta
relativedelta 子類擴展了 datetime 模塊,為程序員提供了處理與檢索到的詳細信息相關的日期和時間的功能。
這個語句表示我們可以在當前使用的日期時間對象中插入日、月甚至年。它還使程序員能夠使用時間間隔和日期時間對象進行工作。
讓我們考慮下面的例子來正確理解這個概念。
示例:
# importing methods from the datetime module as a base.
import datetime
# importing several methods from the dateutil subclasses.
from dateutil.relativedelta import *
from dateutil.easter import *
from dateutil.parser import *
from dateutil.rrule import *
# Creating some datetime objects
present_datetime = datetime.datetime.now()
print("The Present datetime:", present_datetime)
present_date = datetime.date.today()
print("The Present date:", present_date)
輸出:
The Present datetime: 2021-10-20 12:38:19.319256
The Present date: 2021-10-20
說明:
在上面的代碼片段中,我們已經導入了所需的模塊,並創建了日期時間模塊的對象來檢索當前日期和時間的詳細信息。然後,我們為用戶打印了這些詳細信息。
現在,讓我們藉助相對日期來獲取信息。
示例:
# importing methods from the datetime module as a base.
import datetime
# importing several methods from the dateutil subclasses.
from dateutil.relativedelta import *
from dateutil.easter import *
from dateutil.parser import *
from dateutil.rrule import *
# Creating some datetime objects
present_datetime = datetime.datetime.now()
present_date = datetime.date.today()
# Next month
print("Next month:", present_datetime + relativedelta(months =+ 1))
# Next month, and a week
print("Next month, and a week:", present_datetime + relativedelta(months =+ 1, weeks =+ 1))
# Next month, and a week, at 4 PM
print("Next month, plus one week, at 4 PM:", present_datetime + relativedelta(months =+ 1, weeks =+ 1, hour = 16))
# Next Friday
print("Next Friday:", present_date + relativedelta(weekday = FR))
輸出:
Next month: 2021-11-20 12:54:38.805725
Next month, and a week: 2021-11-27 12:54:38.805725
Next month, plus one week, at 4 PM: 2021-11-27 16:54:38.805725
Next Friday: 2021-10-22
說明:
在上面的代碼片段中,我們已經導入了所需的模塊,並使用當前日期和時間的值來查找相對增量的信息,如下個月的日期和時間、一個月和一周、一個月和一周的下午 4 點以及即將到來的星期五的日期。
這個模塊更適用的用途是在一些小操作的幫助下找出信息。
讓我們考慮下面的例子來理解同樣的事情。
示例:
# importing methods from the datetime module as a base.
import datetime
# importing several methods from the dateutil subclasses.
from dateutil.relativedelta import *
from dateutil.easter import *
from dateutil.parser import *
from dateutil.rrule import *
# Creating some datetime objects
present_datetime = datetime.datetime.now()
present_date = datetime.date.today()
# Finding out the last Monday in the month
print("Last Monday:",present_date + relativedelta(day = 31, weekday = MO(-1)))
# We can also work with datetime objects directly
# Example: Age of David
david_birthday = datetime.datetime(1983, 5, 23, 12, 0)
print("David's Age:", relativedelta(present_datetime, david_birthday).years)
輸出:
Last Monday: 2021-10-25
David's Age: 38
說明:
在上面的代碼片段中,我們已經導入了所需的模塊。然後,我們使用當前日期和時間的值,以便根據某人的生日找到有關該月最後一天或其年齡的信息。
日期時間和復活節
復活節子類用於使用通用復活節日曆估計日期和時間,允許程序員計算與各種日曆相關的日期時間對象。
子類非常小,只有一個參數和三個定義整個模塊的選項。
儒略曆,復活節= 1
公曆,復活節 _ 東正教= 2
西曆復活節 _ 西曆= 3
讓我們考慮下面的例子來理解上面提到的日曆的工作。
示例:
# importing methods from the datetime module as a base.
import datetime
# importing several methods from the dateutil subclasses.
from dateutil.relativedelta import *
from dateutil.easter import *
from dateutil.parser import *
from dateutil.rrule import *
# The Julian Calendar
print("Julian Calendar:", easter(1335, 1))
# The Gregorian Calendar
print("Gregorian Calendar:", easter(1335, 2))
# The Western Calendar
print("Western Calendar:", easter(1335, 3))
輸出:
Julian Calendar: 1335-04-16
Gregorian Calendar: 1335-04-26
Western Calendar: 1335-04-17
說明:
在上面的代碼片段中,我們已經導入了所需的模塊,並使用復活節子類以不同的日曆格式打印日期。
日期時間和解析器
解析器子類用作高級日期和時間字符串解析器。這個子類能夠解析一個以上描述日期或時間的已知格式。
讓我們考慮下面的例子來理解同樣的事情。
示例:
# importing methods from the datetime module as a base.
import datetime
# importing several methods from the dateutil subclasses.
from dateutil.relativedelta import *
from dateutil.easter import *
from dateutil.parser import *
from dateutil.rrule import *
# The parser subclass
print(parse("Thu Oct 25 11:36:28 BRST 2005"))
# We can also ignore the timezone which is set to default locally
print(parse("Thu Oct 25 11:36:28 BRST 2005", ignoretz = True))
# We can also not provide a timezone, or a year
# This allows for it to return the current year, with no timezone inclusion.
print(parse("Thu Oct 25 11:36:28"))
# We can also provide variables which consist of the information, as values.
DEFAULT = datetime.datetime(2021, 10, 23)
print(parse("11:36", default = DEFAULT))
輸出:
2005-10-25 11:36:28
2005-10-25 11:36:28
2021-10-25 11:36:28
2021-10-23 11:36:00
說明:
在上面的代碼片段中,我們再次導入了所需的模塊,並使用解析器子類來解析日期和時間字符串。
我們可以提供許多選擇,包括時區、本地或明確的。
我們可以使用傳遞到函數中的變量作為默認參數來提取詳細信息,以便提供時區、年份和時間。
日期時間和規則
rrule 子類用於分別收集關於日期時間對象和日期時間對象的重現的信息。
讓我們考慮下面的例子來理解同樣的事情。
示例:
# importing methods from the datetime module as a base.
import datetime
# importing several methods from the dateutil subclasses.
from dateutil.relativedelta import *
from dateutil.easter import *
from dateutil.parser import *
from dateutil.rrule import *
# The rrule subclass
# Daily repetition for 15 occurrences
print(list(rrule(DAILY, count = 15, dtstart = parse("20210925T091000"))))
# Repeating based on the interval
print(list(rrule(DAILY, interval = 8, count = 4, dtstart = parse("20210925T091000"))))
# Weekly repetition
print(list(rrule(WEEKLY, count = 5, dtstart = parse("20210925T091000"))))
# Monthly repetition
print(list(rrule(MONTHLY, count = 5, dtstart = parse("20210925T091000"))))
# Yearly repetition
print(list(rrule(YEARLY, count = 5, dtstart = parse("20210925T091000"))))
輸出:
[datetime.datetime(2021, 9, 25, 9, 10), datetime.datetime(2021, 9, 26, 9, 10), datetime.datetime(2021, 9, 27, 9, 10), datetime.datetime(2021, 9, 28, 9, 10), datetime.datetime(2021, 9, 29, 9, 10), datetime.datetime(2021, 9, 30, 9, 10), datetime.datetime(2021, 10, 1, 9, 10), datetime.datetime(2021, 10, 2, 9, 10), datetime.datetime(2021, 10, 3, 9, 10), datetime.datetime(2021, 10, 4, 9, 10), datetime.datetime(2021, 10, 5, 9, 10), datetime.datetime(2021, 10, 6, 9, 10), datetime.datetime(2021, 10, 7, 9, 10), datetime.datetime(2021, 10, 8, 9, 10), datetime.datetime(2021, 10, 9, 9, 10)]
[datetime.datetime(2021, 9, 25, 9, 10), datetime.datetime(2021, 10, 3, 9, 10), datetime.datetime(2021, 10, 11, 9, 10), datetime.datetime(2021, 10, 19, 9, 10)]
[datetime.datetime(2021, 9, 25, 9, 10), datetime.datetime(2021, 10, 2, 9, 10), datetime.datetime(2021, 10, 9, 9, 10), datetime.datetime(2021, 10, 16, 9, 10), datetime.datetime(2021, 10, 23, 9, 10)]
[datetime.datetime(2021, 9, 25, 9, 10), datetime.datetime(2021, 10, 25, 9, 10), datetime.datetime(2021, 11, 25, 9, 10), datetime.datetime(2021, 12, 25, 9, 10), datetime.datetime(2022, 1, 25, 9, 10)]
[datetime.datetime(2021, 9, 25, 9, 10), datetime.datetime(2022, 9, 25, 9, 10), datetime.datetime(2023, 9, 25, 9, 10), datetime.datetime(2024, 9, 25, 9, 10), datetime.datetime(2025, 9, 25, 9, 10)]
說明:
在上面的代碼片段中,我們再次導入了所需的模塊。然後,我們使用 rrule 子類來收集關於日期時間對象的重現的信息。
dateutil 模塊的一大功能是,這個子類可以讓程序員處理多個調度任務和日曆存儲創新。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/243443.html