Python 标准库
*************

Python语言参考 描述了 Python 语言的具体语法和语义，这份库参考则介绍了
与 Python 一同发行的标准库。它还描述了通常包含在 Python 发行版中的一些
可选组件。

Python 标准库非常庞大，所提供的组件涉及范围十分广泛，正如以下内容目录
所显示的。这个库包含了多个内置模块 (以 C 编写)，Python 程序员必须依靠
它们来实现系统级功能，例如文件 I/O，此外还有大量以 Python 编写的模块，
提供了日常编程中许多问题的标准解决方案。其中有些模块经过专门设计，通过
将特定平台功能抽象化为平台中立的 API 来鼓励和加强 Python 程序的可移植
性。

Windows 版本的 Python 安装程序通常包含整个标准库，往往还包含许多额外组
件。对于类 Unix 操作系统，Python 通常会分成一系列的软件包，因此可能需
要使用操作系统所提供的包管理工具来获取部分或全部可选组件。

在这个标准库以外还存在成千上万并且不断增加的其他组件 (从单独的程序、模
块、软件包直到完整的应用开发框架)，访问 Python 包索引 即可获取这些第三
方包。

* 1. 概述

* 2. 内置函数

* 3. Non-essential Built-in Functions

* 4. 内置常量

  * 4.1. 由 "site" 模块添加的常量

* 5. 内置类型

  * 5.1. 逻辑值检测

  * 5.2. Boolean Operations — "and", "or", "not"

  * 5.3. 比较运算

  * 5.4. Numeric Types — "int", "float", "long", "complex"

  * 5.5. 迭代器类型

  * 5.6. Sequence Types — "str", "unicode", "list", "tuple",
    "bytearray", "buffer", "xrange"

  * 5.7. 集合类型 — "set", "frozenset"

  * 5.8. 映射类型 — "dict"

  * 5.9. File Objects

  * 5.10. memoryview type

  * 5.11. 上下文管理器类型

  * 5.12. 其他内置类型

  * 5.13. 特殊属性

* 6. 内置异常

  * 6.1. 异常层次结构

* 7. String Services

  * 7.1. "string" — 常见的字符串操作

  * 7.2. "re" — 正则表达式操作

  * 7.3. "struct" — Interpret strings as packed binary data

  * 7.4. "difflib" — 计算差异的辅助工具

  * 7.5. "StringIO" — Read and write strings as files

  * 7.6. "cStringIO" — Faster version of "StringIO"

  * 7.7. "textwrap" — 文本自动换行与填充

  * 7.8. "codecs" — 编解码器注册和相关基类

  * 7.9. "unicodedata" — Unicode 数据库

  * 7.10. "stringprep" — 因特网字符串预备

  * 7.11. "fpformat" — Floating point conversions

* 8. 数据类型

  * 8.1. "datetime" — 基本的日期和时间类型

  * 8.2. "calendar" — 日历相关函数

  * 8.3. "collections" — High-performance container datatypes

  * 8.4. "heapq" — 堆队列算法

  * 8.5. "bisect" — 数组二分查找算法

  * 8.6. "array" — 高效的数值数组

  * 8.7. "sets" — Unordered collections of unique elements

  * 8.8. "sched" — 事件调度器

  * 8.9. "mutex" — Mutual exclusion support

  * 8.10. "Queue" — A synchronized queue class

  * 8.11. "weakref" — 弱引用

  * 8.12. "UserDict" — Class wrapper for dictionary objects

  * 8.13. "UserList" — Class wrapper for list objects

  * 8.14. "UserString" — Class wrapper for string objects

  * 8.15. "types" — Names for built-in types

  * 8.16. "new" — Creation of runtime internal objects

  * 8.17. "copy" — 浅层 (shallow) 和深层 (deep) 复制操作

  * 8.18. "pprint" — 数据美化输出

  * 8.19. "repr" — Alternate "repr()" implementation

* 9. 数字和数学模块

  * 9.1. "numbers" — 数字的抽象基类

  * 9.2. "math" — 数学函数

  * 9.3. "cmath" ——关于复数的数学函数

  * 9.4. "decimal" — 十进制定点和浮点运算

  * 9.5. "fractions" — 分数

  * 9.6. "random" — 生成伪随机数

  * 9.7. "itertools" — 为高效循环而创建迭代器的函数

  * 9.8. "functools" — 高阶函数和可调用对象上的操作

  * 9.9. "operator" — 标准运算符替代函数

* 10. 文件和目录访问

  * 10.1. "os.path" — 常见路径操作

  * 10.2. "fileinput" — Iterate over lines from multiple input
    streams

  * 10.3. "stat" — Interpreting "stat()" results

  * 10.4. "statvfs" — Constants used with "os.statvfs()"

  * 10.5. "filecmp" — 文件及目录的比较

  * 10.6. "tempfile" — Generate temporary files and directories

  * 10.7. "glob" — Unix style pathname pattern expansion

  * 10.8. "fnmatch" — Unix filename pattern matching

  * 10.9. "linecache" — 随机读写文本行

  * 10.10. "shutil" — High-level file operations

  * 10.11. "dircache" — Cached directory listings

  * 10.12. "macpath" — Mac OS 9 路径操作函数

* 11. 数据持久化

  * 11.1. "pickle" —— Python 对象序列化

  * 11.2. "cPickle" — A faster "pickle"

  * 11.3. "copy_reg" — Register "pickle" support functions

  * 11.4. "shelve" — Python object persistence

  * 11.5. "marshal" — Internal Python object serialization

  * 11.6. "anydbm" — Generic access to DBM-style databases

  * 11.7. "whichdb" — Guess which DBM module created a database

  * 11.8. "dbm" — Simple “database” interface

  * 11.9. "gdbm" — GNU’s reinterpretation of dbm

  * 11.10. "dbhash" — DBM-style interface to the BSD database
    library

  * 11.11. "bsddb" — Interface to Berkeley DB library

  * 11.12. "dumbdbm" — Portable DBM implementation

  * 11.13. "sqlite3" — SQLite 数据库 DB-API 2.0 接口模块

* 12. 数据压缩和存档

  * 12.1. "zlib" — 与 **gzip** 兼容的压缩

  * 12.2. "gzip" — 对 **gzip** 格式的支持

  * 12.3. "bz2" — Compression compatible with **bzip2**

  * 12.4. "zipfile" — 使用ZIP存档

  * 12.5. "tarfile" — 读写tar归档文件

* 13. 文件格式

  * 13.1. "csv" — CSV 文件读写

  * 13.2. "ConfigParser" — Configuration file parser

  * 13.3. "robotparser" —  Parser for robots.txt

  * 13.4. "netrc" — netrc file processing

  * 13.5. "xdrlib" — Encode and decode XDR data

  * 13.6. "plistlib" — Generate and parse Mac OS X ".plist" files

* 14. 加密服务

  * 14.1. "hashlib" — 安全哈希与消息摘要

  * 14.2. "hmac" — 基于密钥的消息验证

  * 14.3. "md5" — MD5 message digest algorithm

  * 14.4. "sha" — SHA-1 message digest algorithm

* 15. 通用操作系统服务

  * 15.1. "os" — 操作系统接口模块

  * 15.2. "io" — 处理流的核心工具

  * 15.3. "time" — 时间的访问和转换

  * 15.4. "argparse" — 命令行选项、参数和子命令解析器

  * 15.5. "optparse" — 解析器的命令行选项

  * 15.6. "getopt" — C-style parser for command line options

  * 15.7. 模块 "logging" — Python 的日志记录工具

  * 15.8. "logging.config" — 日志记录配置

  * 15.9. "logging.handlers" — Logging handlers

  * 15.10. "getpass" — 便携式密码输入工具

  * 15.11. "curses" — 终端字符单元显示的处理

  * 15.12. "curses.textpad" — Text input widget for curses programs

  * 15.13. "curses.ascii" — Utilities for ASCII characters

  * 15.14. "curses.panel" — A panel stack extension for curses

  * 15.15. "platform" —  获取底层平台的标识数据

  * 15.16. "errno" — Standard errno system symbols

  * 15.17. "ctypes" — Python 的外部函数库

* 16. Optional Operating System Services

  * 16.1. "select" — Waiting for I/O 完成

  * 16.2. "threading" — Higher-level threading interface

  * 16.3. "thread" — Multiple threads of control

  * 16.4. "dummy_threading" —  可直接替代 "threading" 模块。

  * 16.5. "dummy_thread" — Drop-in replacement for the "thread"
    module

  * 16.6. "multiprocessing" — Process-based “threading” interface

  * 16.7. "mmap" — 内存映射文件支持

  * 16.8. "readline" — GNU readline 接口

  * 16.9. "rlcompleter" — GNU readline 的补全函数

* 17. Interprocess Communication and Networking

  * 17.1. "subprocess" — 子进程管理

  * 17.2. "socket" — 底层网络接口

  * 17.3. "ssl" — TLS/SSL wrapper for socket objects

  * 17.4. "signal" — 设置异步事件处理程序

  * 17.5. "popen2" — Subprocesses with accessible I/O streams

  * 17.6. "asyncore" — 异步socket处理器

  * 17.7. "asynchat" — 异步 socket 指令/响应 处理器

* 18. 互联网数据处理

  * 18.1. "email" — 电子邮件与 MIME 处理包

  * 18.2. "json" — JSON 编码和解码器

  * 18.3. "mailcap" — Mailcap file handling

  * 18.4. "mailbox" — Manipulate mailboxes in various formats

  * 18.5. "mhlib" — Access to MH mailboxes

  * 18.6. "mimetools" — Tools for parsing MIME messages

  * 18.7. "mimetypes" — Map filenames to MIME types

  * 18.8. "MimeWriter" — Generic MIME file writer

  * 18.9. "mimify" — MIME processing of mail messages

  * 18.10. "multifile" — Support for files containing distinct parts

  * 18.11. "rfc822" — Parse RFC 2822 mail headers

  * 18.12. "base64" — RFC 3548: Base16, Base32, Base64 Data
    Encodings

  * 18.13. "binhex" — 对binhex4文件进行编码和解码

  * 18.14. "binascii" — 二进制和 ASCII 码互转

  * 18.15. "quopri" — 编码与解码经过 MIME 转码的可打印数据

  * 18.16. "uu" — 对 uuencode 文件进行编码与解码

* 19. 结构化标记处理工具

  * 19.1. "HTMLParser" — Simple HTML and XHTML parser

  * 19.2. "sgmllib" — Simple SGML parser

  * 19.3. "htmllib" — A parser for HTML documents

  * 19.4. "htmlentitydefs" — Definitions of HTML general entities

  * 19.5. XML处理模块

  * 19.6. XML 漏洞

  * 19.7. "xml.etree.ElementTree" —  ElementTree XML API

  * 19.8. "xml.dom" — The Document Object Model API

  * 19.9. "xml.dom.minidom" — Minimal DOM implementation

  * 19.10. "xml.dom.pulldom" — Support for building partial DOM
    trees

  * 19.11. "xml.sax" — Support for SAX2 parsers

  * 19.12. "xml.sax.handler" — Base classes for SAX handlers

  * 19.13. "xml.sax.saxutils" — SAX Utilities

  * 19.14. "xml.sax.xmlreader" — Interface for XML parsers

  * 19.15. "xml.parsers.expat" — Fast XML parsing using Expat

* 20. 互联网协议和支持

  * 20.1. "webbrowser" — 方便的Web浏览器控制器

  * 20.2. "cgi" — Common Gateway Interface support

  * 20.3. "cgitb" — 用于 CGI 脚本的回溯管理器

  * 20.4. "wsgiref" — WSGI Utilities and Reference Implementation

  * 20.5. "urllib" — Open arbitrary resources by URL

  * 20.6. "urllib2" — extensible library for opening URLs

  * 20.7. "httplib" — HTTP protocol client

  * 20.8. "ftplib" — FTP protocol client

  * 20.9. "poplib" — POP3 protocol client

  * 20.10. "imaplib" — IMAP4 protocol client

  * 20.11. "nntplib" — NNTP protocol client

  * 20.12. "smtplib" —SMTP协议客户端

  * 20.13. "smtpd" — SMTP 服务器

  * 20.14. "telnetlib" — Telnet client

  * 20.15. "uuid" — UUID objects according to RFC 4122

  * 20.16. "urlparse" — Parse URLs into components

  * 20.17. "SocketServer" — A framework for network servers

  * 20.18. "BaseHTTPServer" — Basic HTTP server

  * 20.19. "SimpleHTTPServer" — Simple HTTP request handler

  * 20.20. "CGIHTTPServer" — CGI-capable HTTP request handler

  * 20.21. "cookielib" — Cookie handling for HTTP clients

  * 20.22. "Cookie" — HTTP state management

  * 20.23. "xmlrpclib" — XML-RPC client access

  * 20.24. "SimpleXMLRPCServer" — Basic XML-RPC server

  * 20.25. "DocXMLRPCServer" — Self-documenting XML-RPC server

* 21. 多媒体服务

  * 21.1. "audioop" — Manipulate raw audio data

  * 21.2. "imageop" — Manipulate raw image data

  * 21.3. "aifc" — Read and write AIFF and AIFC files

  * 21.4. "sunau" — 读写 Sun AU 文件

  * 21.5. "wave" — 读写WAV格式文件

  * 21.6. "chunk" — Read IFF chunked data

  * 21.7. "colorsys" — 颜色系统间的转换

  * 21.8. "imghdr" — 推测图像类型

  * 21.9. "sndhdr" — 推测声音文件的类型

  * 21.10. "ossaudiodev" — Access to OSS-compatible audio devices

* 22. 国际化

  * 22.1. "gettext" — 多语种国际化服务

  * 22.2. "locale" — 国际化服务

* 23. 程序框架

  * 23.1. "cmd" — 支持面向行的命令解释器

  * 23.2. "shlex" — Simple lexical analysis

* 24. Tk图形用户界面(GUI)

  * 24.1. "Tkinter" — Python interface to Tcl/Tk

  * 24.2. "ttk" — Tk themed widgets

  * 24.3. "Tix" — Extension widgets for Tk

  * 24.4. "ScrolledText" — Scrolled Text Widget

  * 24.5. "turtle" — Turtle graphics for Tk

  * 24.6. IDLE

  * 24.7. 其他图形用户界面（GUI）包

* 25. 开发工具

  * 25.1. "pydoc" — Documentation generator and online help system

  * 25.2. "doctest" — 测试交互性的Python示例

  * 25.3. "unittest" — 单元测试框架

  * 25.4. 2to3 - 自动将 Python 2 代码转为 Python 3 代码

  * 25.5. "test" — Regression tests package for Python

  * 25.6. "test.support" — Utility functions for tests

* 26. 调试和分析

  * 26.1. "bdb" — Debugger framework

  * 26.2. "pdb" — Python的调试器

  * 26.3. Debugger Commands

  * 26.4. The Python Profilers

  * 26.5. "hotshot" — High performance logging profiler

  * 26.6. "timeit" — 测量小代码片段的执行时间

  * 26.7. "trace" — Trace or track Python statement execution

* 27. 软件打包和分发

  * 27.1. "distutils" — 构建和安装 Python 模块

  * 27.2. "ensurepip" — Bootstrapping the "pip" installer

* 28. Python运行时服务

  * 28.1. "sys" — 系统相关的参数和函数

  * 28.2. "sysconfig" — Provide access to Python’s configuration
    information

  * 28.3. "__builtin__" — Built-in objects

  * 28.4. "future_builtins" — Python 3 builtins

  * 28.5. "__main__" — 顶层脚本环境

  * 28.6. "warnings" — Warning control

  * 28.7. "contextlib" — Utilities for "with"-statement contexts

  * 28.8. "abc" — 抽象基类

  * 28.9. "atexit" — 退出处理器

  * 28.10. "traceback" — 打印或检索堆栈回溯

  * 28.11. "__future__" — Future 语句定义

  * 28.12. "gc" — 垃圾回收器接口

  * 28.13. "inspect" — 检查对象

  * 28.14. "site" — Site-specific configuration hook

  * 28.15. "user" — User-specific configuration hook

  * 28.16. "fpectl" — Floating point exception control

* 29. 自定义 Python 解释器

  * 29.1. "code" — 解释器基础类

  * 29.2. "codeop" — 编译Python代码

* 30. Restricted Execution

  * 30.1. "rexec" — Restricted execution framework

  * 30.2. "Bastion" — Restricting access to objects

* 31. 导入模块

  * 31.1. "imp" — Access the "import" internals

  * 31.2. "importlib" — Convenience wrappers for "__import__()"

  * 31.3. "imputil" — Import utilities

  * 31.4. "zipimport" — Import modules from Zip archives

  * 31.5. "pkgutil" — Package extension utility

  * 31.6. "modulefinder" — 查找脚本使用的模块

  * 31.7. "runpy" — Locating and executing Python modules

* 32. Python 语言服务

  * 32.1. "parser" — Access Python parse trees

  * 32.2. "ast" — 抽象语法树

  * 32.3. "symtable" — Access to the compiler’s symbol tables

  * 32.4. "symbol" — 与 Python 解析树一起使用的常量

  * 32.5. "token" — 与Python解析树一起使用的常量

  * 32.6. "keyword" — 检验Python关键字

  * 32.7. "tokenize" — Tokenizer for Python source

  * 32.8. "tabnanny" — 模糊缩进检测

  * 32.9. "pyclbr" — Python class browser support

  * 32.10. "py_compile" — Compile Python source files

  * 32.11. "compileall" — Byte-compile Python libraries

  * 32.12. "dis" — Python 字节码反汇编器

  * 32.13. "pickletools" — Tools for pickle developers

* 33. Python compiler package

  * 33.1. The basic interface

  * 33.2. Limitations

  * 33.3. Python Abstract Syntax

  * 33.4. Using Visitors to Walk ASTs

  * 33.5. Bytecode Generation

* 34. 杂项服务

  * 34.1. "formatter" — Generic output formatting

* 35. Windows系统相关模块

  * 35.1. "msilib" — Read and write Microsoft Installer files

  * 35.2. "msvcrt" — Useful routines from the MS VC++ runtime

  * 35.3. "_winreg" — Windows registry access

  * 35.4. "winsound" — Sound-playing interface for Windows

* 36. Unix 专有服务

  * 36.1. "posix" — The most common POSIX system calls

  * 36.2. "pwd" — 用户密码数据库

  * 36.3. "spwd" — The shadow password database

  * 36.4. "grp" — The group database

  * 36.5. "crypt" — Function to check Unix passwords

  * 36.6. "dl" — Call C functions in shared objects

  * 36.7. "termios" — POSIX style tty control

  * 36.8. "tty" — 终端控制功能

  * 36.9. "pty" — Pseudo-terminal utilities

  * 36.10. "fcntl" — The "fcntl" and "ioctl" system calls

  * 36.11. "pipes" — Interface to shell pipelines

  * 36.12. "posixfile" — File-like objects with locking support

  * 36.13. "resource" — Resource usage information

  * 36.14. "nis" — Interface to Sun’s NIS (Yellow Pages)

  * 36.15. Unix syslog 库例程

  * 36.16. "commands" — Utilities for running commands

* 37. Mac OS X specific services

  * 37.1. "ic" — Access to the Mac OS X Internet Config

  * 37.2. "MacOS" — Access to Mac OS interpreter features

  * 37.3. "macostools" — Convenience routines for file manipulation

  * 37.4. "findertools" — The **finder**’s Apple Events interface

  * 37.5. "EasyDialogs" — Basic Macintosh dialogs

  * 37.6. "FrameWork" — Interactive application framework

  * 37.7. "autoGIL" — Global Interpreter Lock handling in event
    loops

  * 37.8. Mac OS Toolbox Modules

  * 37.9. "ColorPicker" — Color selection dialog

* 38. MacPython OSA Modules

  * 38.1. "gensuitemodule" — Generate OSA stub packages

  * 38.2. "aetools" — OSA client support

  * 38.3. "aepack" — Conversion between Python variables and
    AppleEvent data containers

  * 38.4. "aetypes" — AppleEvent objects

  * 38.5. "MiniAEFrame" — Open Scripting Architecture server support

* 39. SGI IRIX Specific Services

  * 39.1. "al" — Audio functions on the SGI

  * 39.2. "AL" — Constants used with the "al" module

  * 39.3. "cd" — CD-ROM access on SGI systems

  * 39.4. "fl" — FORMS library for graphical user interfaces

  * 39.5. "FL" — Constants used with the "fl" module

  * 39.6. "flp" — Functions for loading stored FORMS designs

  * 39.7. "fm" — *Font Manager* interface

  * 39.8. "gl" — *Graphics Library* interface

  * 39.9. "DEVICE" — Constants used with the "gl" module

  * 39.10. "GL" — Constants used with the "gl" module

  * 39.11. "imgfile" — Support for SGI imglib files

  * 39.12. "jpeg" — Read and write JPEG files

* 40. SunOS Specific Services

  * 40.1. "sunaudiodev" — Access to Sun audio hardware

  * 40.2. "SUNAUDIODEV" — Constants used with "sunaudiodev"

* 41. 未创建文档的模块

  * 41.1. Miscellaneous useful utilities

  * 41.2. 平台特定模块

  * 41.3. Multimedia

  * 41.4. Undocumented Mac OS modules

  * 41.5. Obsolete

  * 41.6. SGI-specific Extension modules
