Python 標準庫概述介紹(每日一庫)

這篇文章主要給大家介紹一下Python標準庫,瞭解每種庫的大概用途,在以後每日一庫中,會詳細介紹每種庫的在開發中實際用處,讓大家在以後開發中,能夠想到用那種庫解決問題。

Python 標準庫非常龐大,所提供的組件涉及範圍十分廣泛,正如以下內容目錄所顯示的。這個庫包含了多個內置模塊 (以 C 編寫),Python 程序員必須依靠它們來實現系統級功能,例如文件 I/O,此外還有大量以 Python 編寫的模塊,提供了日常編程中許多問題的標準解決方案。其中有些模塊經過專門設計,通過將特定平臺功能抽象化爲平臺中立的 API 來鼓勵和加強 Python 程序的可移植性。

Windows 版本的 Python 安裝程序通常包含整個標準庫,往往還包含許多額外組件。對於類 Unix 操作系統,Python 通常會分成一系列的軟件包,因此可能需要使用操作系統所提供的包管理工具來獲取部分或全部可選組件。

在這個標準庫以外還存在成千上萬並且不斷增加的其他組件 (從單獨的程序、模塊、軟件包直到完整的應用開發框架),訪Python 包索引 即可獲取這些第三方包。

 

概述


可用性註釋


內置函數


內置常量


由 site 模塊添加的常量


內置類型


邏輯值檢測
布爾運算 --- and, or, not
比較運算
數字類型 --- int, float, complex
迭代器類型
序列類型 --- list, tuple, range
文本序列類型 --- str
二進制序列類型 --- bytes, bytearray, memoryview
集合類型 --- set, frozenset
映射類型 --- dict
上下文管理器類型
其他內置類型
特殊屬性


內置異常


基類
具體異常
警告
異常層次結構


文本處理服務


string --- 常見的字符串操作
re --- 正則表達式操作
difflib --- 計算差異的輔助工具
textwrap --- 文本自動換行與填充
unicodedata --- Unicode 數據庫
stringprep --- 因特網字符串預備
readline --- GNU readline 接口
rlcompleter --- GNU readline 的補全函數


二進制數據服務


struct --- 將字節串解讀爲打包的二進制數據
codecs --- 編解碼器註冊和相關基類


數據類型


datetime --- 基本的日期和時間類型
calendar --- 日曆相關函數
collections --- 容器數據類型
collections.abc --- 容器的抽象基類
heapq --- 堆隊列算法
bisect --- 數組二分查找算法
array --- 高效的數值數組
weakref --- 弱引用
types --- 動態類型創建和內置類型名稱
copy --- 淺層 (shallow) 和深層 (deep) 複製操作
pprint --- 數據美化輸出
reprlib --- 另一種 repr() 實現
enum --- 枚舉類型支持


數字和數學模塊


numbers --- 數字的抽象基類
math --- 數學函數
cmath ——關於複數的數學函數
decimal --- 十進制定點和浮點運算
fractions --- 分數
random --- 生成僞隨機數
statistics --- 數學統計函數


函數式編程模塊


itertools --- 爲高效循環而創建迭代器的函數
functools --- 高階函數和可調用對象上的操作
operator --- 標準運算符替代函數


文件和目錄訪問


pathlib --- 面向對象的文件系統路徑
os.path --- 常見路徑操作
fileinput --- Iterate over lines from multiple input streams
stat --- Interpreting stat() results
filecmp --- 文件及目錄的比較
tempfile --- Generate temporary files and directories
glob --- Unix style pathname pattern expansion
fnmatch --- Unix filename pattern matching
linecache --- Random access to text lines
shutil --- High-level file operations
macpath --- Mac OS 9 路徑操作函數


數據持久化


pickle —— Python 對象序列化
copyreg --- 注意 pickle 支持函數
shelve --- Python object persistence
marshal --- Internal Python object serialization
dbm --- Interfaces to Unix "databases"
sqlite3 --- SQLite 數據庫 DB-API 2.0 接口模塊


數據壓縮和存檔


zlib --- 與 gzip 兼容的壓縮
gzip --- 對 gzip 格式的支持
bz2 --- 對 bzip2 壓縮算法的支持
lzma --- 用 LZMA 算法壓縮
zipfile --- 使用ZIP存檔
tarfile --- 讀寫tar歸檔文件


文件格式


csv --- CSV 文件讀寫
configparser --- Configuration file parser
netrc --- netrc file processing
xdrlib --- Encode and decode XDR data
plistlib --- Generate and parse Mac OS X .plist files


加密服務


hashlib --- 安全哈希與消息摘要
hmac --- 基於密鑰的消息驗證
secrets --- Generate secure random numbers for managing secrets


通用操作系統服務


os --- 操作系統接口模塊
io --- 處理流的核心工具
time --- 時間的訪問和轉換
argparse --- 命令行選項、參數和子命令解析器
getopt --- C-style parser for command line options
模塊 logging --- Python 的日誌記錄工具
logging.config --- 日誌記錄配置
logging.handlers --- Logging handlers
getpass --- 便攜式密碼輸入工具
curses --- 終端字符單元顯示的處理
curses.textpad --- Text input widget for curses programs
curses.ascii --- Utilities for ASCII characters
curses.panel --- A panel stack extension for curses
platform --- 獲取底層平臺的標識數據
errno --- Standard errno system symbols
ctypes --- Python 的外部函數庫


併發執行


threading --- 基於線程的並行
multiprocessing --- 基於進程的並行
concurrent 包
concurrent.futures --- 啓動並行任務
subprocess --- 子進程管理
sched --- 事件調度器
queue --- 一個同步的隊列類
_thread --- 底層多線程 API
_dummy_thread --- _thread 的替代模塊
dummy_threading --- 可直接替代 threading 模塊


contextvars --- Context Variables


Context Variables
Manual Context Management
asyncio support


網絡和進程間通信


asyncio --- 異步 I/O
socket --- 底層網絡接口
ssl --- TLS/SSL wrapper for socket objects
select --- Waiting for I/O completion
selectors --- 高級 I/O 複用庫
asyncore --- 異步socket處理器
asynchat --- 異步 socket 指令/響應 處理器
signal --- 設置異步事件處理程序
mmap --- 內存映射文件支持


互聯網數據處理


email --- 電子郵件與 MIME 處理包
json --- JSON 編碼和解碼器
mailcap --- Mailcap file handling
mailbox --- Manipulate mailboxes in various formats
mimetypes --- Map filenames to MIME types
base64 --- Base16, Base32, Base64, Base85 數據編碼
binhex --- 對binhex4文件進行編碼和解碼
binascii --- 二進制和 ASCII 碼互轉
quopri --- 編碼與解碼經過 MIME 轉碼的可打印數據
uu --- 對 uuencode 文件進行編碼與解碼


結構化標記處理工具


html --- 超文本標記語言支持
html.parser --- 簡單的 HTML 和 XHTML 解析器
html.entities --- HTML 一般實體的定義
XML處理模塊
xml.etree.ElementTree --- ElementTree XML API
xml.dom --- The Document Object Model API
xml.dom.minidom --- Minimal DOM implementation
xml.dom.pulldom --- Support for building partial DOM trees
xml.sax --- Support for SAX2 parsers
xml.sax.handler --- Base classes for SAX handlers
xml.sax.saxutils --- SAX Utilities
xml.sax.xmlreader --- Interface for XML parsers
xml.parsers.expat --- Fast XML parsing using Expat


互聯網協議和支持


webbrowser --- 方便的Web瀏覽器控制器
cgi --- Common Gateway Interface support
cgitb --- Traceback manager for CGI scripts
wsgiref --- WSGI Utilities and Reference Implementation
urllib --- URL 處理模塊
urllib.request --- 用於打開 URL 的可擴展庫
urllib.response --- urllib 使用的 Response 類
urllib.parse --- Parse URLs into components
urllib.error --- urllib.request 引發的異常類
urllib.robotparser --- robots.txt 語法分析程序
http --- HTTP 模塊
http.client --- HTTP 協議客戶端
ftplib --- FTP protocol client
poplib --- POP3 protocol client
imaplib --- IMAP4 protocol client
nntplib --- NNTP protocol client
smtplib ---SMTP協議客戶端
smtpd --- SMTP 服務器
telnetlib --- Telnet client
uuid --- UUID objects according to RFC 4122
socketserver --- A framework for network servers
http.server --- HTTP 服務器
http.cookies --- HTTP狀態管理
http.cookiejar —— HTTP 客戶端的 Cookie 處理
xmlrpc --- XMLRPC 服務端與客戶端模塊
xmlrpc.client --- XML-RPC client access
xmlrpc.server --- Basic XML-RPC servers
ipaddress --- IPv4/IPv6 manipulation library


多媒體服務


audioop --- Manipulate raw audio data
aifc --- Read and write AIFF and AIFC files
sunau --- 讀寫 Sun AU 文件
wave --- 讀寫WAV格式文件
chunk --- Read IFF chunked data
colorsys --- 顏色系統間的轉換
imghdr --- 推測圖像類型
sndhdr --- 推測聲音文件的類型
ossaudiodev --- Access to OSS-compatible audio devices


國際化


gettext --- 多語種國際化服務
locale --- 國際化服務


程序框架


turtle --- 海龜繪圖
cmd --- 支持面向行的命令解釋器
shlex --- Simple lexical analysis


Tk圖形用戶界面(GUI)


tkinter --- Tcl/Tk的Python接口
tkinter.ttk --- Tk主題小部件
tkinter.tix --- Extension widgets for Tk
tkinter.scrolledtext --- 滾動文字控件
IDLE
其他圖形用戶界面(GUI)包


開發工具


typing --- 類型標註支持
pydoc --- Documentation generator and online help system
doctest --- 測試交互性的Python示例
unittest --- 單元測試框架
unittest.mock --- 模擬對象庫
unittest.mock 上手指南
2to3 - 自動將 Python 2 代碼轉爲 Python 3 代碼
test --- Regression tests package for Python
test.support --- Utilities for the Python test suite
test.support.script_helper --- Utilities for the Python execution tests


調試和分析


bdb --- Debugger framework
faulthandler --- Dump the Python traceback
pdb --- Python的調試器
The Python Profilers
timeit --- 測量小代碼片段的執行時間
trace --- Trace or track Python statement execution
tracemalloc --- Trace memory allocations


軟件打包和分發


distutils --- 構建和安裝 Python 模塊
ensurepip --- Bootstrapping the pip installer
venv --- 創建虛擬環境
zipapp --- Manage executable Python zip archives


Python運行時服務


sys --- 系統相關的參數和函數
sysconfig --- Provide access to Python's configuration information
builtins --- 內建對象
__main__ --- 頂層腳本環境
warnings --- Warning control
dataclasses --- 數據類
contextlib --- Utilities for with-statement contexts
abc --- 抽象基類
atexit --- 退出處理器
traceback --- 打印或檢索堆棧回溯
__future__ --- Future 語句定義
gc --- 垃圾回收器接口
inspect --- 檢查對象
site --- Site-specific configuration hook


自定義 Python 解釋器


code --- Interpreter base classes
codeop --- 編譯Python代碼


導入模塊


zipimport --- Import modules from Zip archives
pkgutil --- Package extension utility
modulefinder --- 查找腳本使用的模塊
runpy --- Locating and executing Python modules
importlib --- import 的實現


Python 語言服務


parser --- Access Python parse trees
ast --- 抽象語法樹
symtable --- Access to the compiler's symbol tables
symbol --- 與 Python 解析樹一起使用的常量
token --- 與Python解析樹一起使用的常量
keyword --- 檢驗Python關鍵字
tokenize --- Tokenizer for Python source
tabnanny --- 模糊縮進檢測
pyclbr --- Python class browser support
py_compile --- Compile Python source files
compileall --- Byte-compile Python libraries
dis --- Python 字節碼反彙編器
pickletools --- Tools for pickle developers


雜項服務


formatter --- Generic output formatting


Windows系統相關模塊


msilib --- Read and write Microsoft Installer files
msvcrt --- Useful routines from the MS VC++ runtime
winreg --- Windows 註冊表訪問
winsound --- Sound-playing interface for Windows


Unix 專有服務


posix --- The most common POSIX system calls
pwd --- 用戶密碼數據庫
spwd --- The shadow password database
grp --- The group database
crypt --- Function to check Unix passwords
termios --- POSIX style tty control
tty --- 終端控制功能
pty --- Pseudo-terminal utilities
fcntl --- The fcntl and ioctl system calls
pipes --- Interface to shell pipelines
resource --- Resource usage information
nis --- Interface to Sun's NIS (Yellow Pages)
Unix syslog 庫例程


被取代的模塊


optparse --- Parser for command line options
imp --- Access the import internals


未創建文檔的模塊


平臺特定模塊

 

進QQ羣(779809018)免費領取學習資源,疑難問題解答。同時歡迎大家關注我的微信公衆號:代碼幫 ,免費領取學習資源。

本公衆號將秉持活到老學到老學習無休止的交流分享開源精神,匯聚於互聯網和個人學習工作的精華乾貨知識,一切來於互聯網,反饋回互聯網。
目前研究領域:大數據、機器學習、深度學習、人工智能、數據挖掘、數據分析。 語言涉及:Java、Scala、Python、Shell、Linux等 。同時還涉及平常所使用的手機、電腦和互聯網上的使用技巧、問題和實用軟件破解。 只要你一直關注和呆在羣裏,每天必須有收穫,討論和答疑QQ羣:大數據和人工智能總羣(779809018)微信公衆號(代碼幫)每天分享最新IT、大數據和人工智能新技術。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章