Files
python/Packages/StandardLibrary/readme.md
2025-09-08 21:11:28 +08:00

167 lines
9.6 KiB
Markdown
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

好的,我来帮你整理一份 **Python 标准库Standard Library详细概览**,按照用途分类,包含核心模块、功能和简单示例,方便快速了解。
Python 标准库是 **Python 自带的模块集合**,无需额外安装即可使用。
---
# 1⃣ 操作系统与文件
| 模块 | 功能 | 示例 |
| ---------- | ---------------- | ------------------------------------------- |
| `os` | 系统操作、文件路径、环境变量 | `import os; os.listdir('.')` |
| `sys` | Python 解释器相关信息 | `import sys; sys.argv` |
| `shutil` | 高级文件操作(复制、移动、删除) | `import shutil; shutil.copy('a','b')` |
| `pathlib` | 面向对象的路径操作 | `from pathlib import Path; p = Path('.')` |
| `tempfile` | 临时文件和目录 | `import tempfile; tempfile.TemporaryFile()` |
| `glob` | 文件通配符匹配 | `import glob; glob.glob('*.py')` |
---
# 2⃣ 数据类型与容器
| 模块 | 功能 | 示例 |
| ------------- | --------------------------------------------------------- | --------------------------------------------------- |
| `collections` | 高级容器类型,如 `Counter`, `deque`, `OrderedDict`, `defaultdict` | `from collections import Counter; Counter([1,2,2])` |
| `array` | 高效数值数组 | `from array import array; a = array('i',[1,2,3])` |
| `heapq` | 堆操作 | `import heapq; heapq.heappush(heap, x)` |
| `bisect` | 有序列表插入和查找 | `import bisect; bisect.insort(lst, 5)` |
| `queue` | 线程安全队列 | `from queue import Queue; q = Queue()` |
---
# 3⃣ 数学与科学计算
| 模块 | 功能 | 示例 |
| ------------ | ------- | --------------------------------------------------------------- |
| `math` | 数学函数 | `import math; math.sqrt(16)` |
| `cmath` | 复数数学函数 | `import cmath; cmath.sqrt(-1)` |
| `decimal` | 高精度浮点运算 | `from decimal import Decimal; Decimal('1.1') + Decimal('2.2')` |
| `fractions` | 分数运算 | `from fractions import Fraction; Fraction(1,3) + Fraction(2,3)` |
| `random` | 随机数生成 | `import random; random.randint(1,10)` |
| `statistics` | 基本统计函数 | `import statistics; statistics.mean([1,2,3])` |
---
# 4⃣ 文本处理
| 模块 | 功能 | 示例 |
| ------------- | ------------ | ------------------------------------------------------- |
| `re` | 正则表达式 | `import re; re.findall(r'\d+', 'abc123')` |
| `string` | 字符串常量与模板 | `import string; string.ascii_letters` |
| `textwrap` | 文本换行与缩进 | `import textwrap; textwrap.fill("long text", width=20)` |
| `unicodedata` | Unicode 字符属性 | `import unicodedata; unicodedata.name('中')` |
| `codecs` | 编码解码 | `import codecs; codecs.encode("abc","utf-8")` |
---
# 5⃣ 数据持久化与格式
| 模块 | 功能 | 示例 |
| ----------------------- | ------------- | ------------------------------------------------------- |
| `json` | JSON 序列化与反序列化 | `import json; json.loads('{"a":1}')` |
| `pickle` | Python 对象序列化 | `import pickle; pickle.dump(obj,f)` |
| `shelve` | 类似字典的文件存储 | `import shelve; db = shelve.open('data')` |
| `csv` | CSV 文件读写 | `import csv; reader = csv.reader(f)` |
| `configparser` | 配置文件解析 | `import configparser; config.read('config.ini')` |
| `xml.etree.ElementTree` | XML 解析 | `import xml.etree.ElementTree as ET; ET.parse('a.xml')` |
---
# 6⃣ 日期与时间
| 模块 | 功能 | 示例 |
| ---------- | ------- | ----------------------------------------------- |
| `datetime` | 日期和时间处理 | `from datetime import datetime; datetime.now()` |
| `time` | 时间戳、延时 | `import time; time.sleep(1)` |
| `calendar` | 日历相关 | `import calendar; calendar.month(2025,9)` |
---
# 7⃣ 网络与互联网
| 模块 | 功能 | 示例 |
| ------------- | --------- | ---------------------------------------------------------------------- |
| `socket` | 网络通信 | `import socket; s = socket.socket()` |
| `http.client` | HTTP 客户端 | `import http.client; conn = http.client.HTTPConnection('example.com')` |
| `urllib` | URL 处理、请求 | `from urllib import request; request.urlopen('http://example.com')` |
| `ftplib` | FTP 协议 | `from ftplib import FTP; ftp = FTP('host')` |
| `smtplib` | 发送邮件 | `import smtplib; server = smtplib.SMTP('smtp.example.com')` |
| `email` | 邮件解析和构建 | `from email.mime.text import MIMEText` |
---
# 8⃣ 并发与多线程
| 模块 | 功能 | 示例 |
| -------------------- | ------ | --------------------------------------------------------------- |
| `threading` | 多线程 | `import threading; t = threading.Thread(target=func)` |
| `multiprocessing` | 多进程 | `from multiprocessing import Process; p = Process(target=func)` |
| `asyncio` | 异步编程 | `import asyncio; asyncio.run(main())` |
| `concurrent.futures` | 线程/进程池 | `from concurrent.futures import ThreadPoolExecutor` |
| `queue` | 线程安全队列 | `from queue import Queue` |
---
# 9⃣ 调试与开发工具
| 模块 | 功能 | 示例 |
| ----------- | ------ | ------------------------------------------ |
| `logging` | 日志记录 | `import logging; logging.info("msg")` |
| `pdb` | 调试 | `import pdb; pdb.set_trace()` |
| `traceback` | 异常堆栈信息 | `import traceback; traceback.format_exc()` |
| `warnings` | 警告控制 | `import warnings; warnings.warn("msg")` |
| `inspect` | 查看对象信息 | `import inspect; inspect.getmembers(obj)` |
---
# 10⃣ 压缩与归档
| 模块 | 功能 | 示例 |
| --------- | -------- | ------------------------------------------ |
| `zipfile` | ZIP 文件读写 | `import zipfile; zipfile.ZipFile('a.zip')` |
| `tarfile` | TAR 文件读写 | `import tarfile; tarfile.open('a.tar.gz')` |
| `gzip` | Gzip 压缩 | `import gzip; gzip.open('a.gz','rb')` |
| `bz2` | bzip2 压缩 | `import bz2` |
| `lzma` | lzma 压缩 | `import lzma` |
---
# 11⃣ 其他常用
| 模块 | 功能 | 示例 |
| ----------- | -------- | ----------------------------------------------------- |
| `uuid` | UUID 生成 | `import uuid; uuid.uuid4()` |
| `hashlib` | 哈希算法 | `import hashlib; hashlib.md5(b'abc').hexdigest()` |
| `hmac` | HMAC 校验 | `import hmac` |
| `enum` | 枚举类型 | `from enum import Enum` |
| `functools` | 函数工具,如缓存 | `from functools import lru_cache` |
| `itertools` | 高级迭代工具 | `import itertools; itertools.combinations([1,2,3],2)` |
---
## 🔹 总结
Python 标准库非常丰富,可以大体分类为:
1. **系统与文件** → os, sys, pathlib, shutil, tempfile
2. **数据结构与容器** → collections, array, heapq, queue
3. **数学与统计** → math, cmath, decimal, fractions, random, statistics
4. **文本处理** → re, string, textwrap, codecs, unicodedata
5. **数据存储与格式** → json, pickle, shelve, csv, configparser, xml
6. **日期与时间** → datetime, time, calendar
7. **网络与互联网** → socket, urllib, http.client, ftplib, smtplib, email
8. **并发与异步** → threading, multiprocessing, asyncio, concurrent.futures
9. **调试与开发** → logging, pdb, traceback, inspect
10. **压缩与归档** → zipfile, tarfile, gzip, bz2, lzma
11. **其他工具** → uuid, hashlib, hmac, enum, functools, itertools
---
💡 提示Python 官方文档有 **完整标准库参考**,可直接查阅每个模块的 API。
---
我可以帮你画一张 **Python 标准库分类全景图**,把模块按功能归类,直观展示,非常适合学习和查阅。
你希望我画吗?