🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
### 导航 - [索引](../genindex.xhtml "总目录") - [模块](../py-modindex.xhtml "Python 模块索引") | - [下一页](binhex.xhtml "binhex --- 对binhex4文件进行编码和解码") | - [上一页](mimetypes.xhtml "mimetypes --- Map filenames to MIME types") | - ![](https://box.kancloud.cn/a721fc7ec672275e257bbbfde49a4d4e_16x16.png) - [Python](https://www.python.org/) » - zh\_CN 3.7.3 [文档](../index.xhtml) » - [Python 标准库](index.xhtml) » - [互联网数据处理](netdata.xhtml) » - $('.inline-search').show(0); | # [`base64`](#module-base64 "base64: RFC 3548: Base16, Base32, Base64 Data Encodings; Base85 and Ascii85") --- Base16, Base32, Base64, Base85 数据编码 **源代码:** [Lib/base64.py](https://github.com/python/cpython/tree/3.7/Lib/base64.py) \[https://github.com/python/cpython/tree/3.7/Lib/base64.py\] - - - - - - 此模块提供了将二进制数据编码为可打印的 ASCII 字符以及将这些编码解码回二进制数据的函数。它为 [**RFC 3548**](https://tools.ietf.org/html/rfc3548.html) \[https://tools.ietf.org/html/rfc3548.html\] 指定的 Base16, Base32 和 Base64 编码以及已被广泛接受的 Ascii85 和 Base85 编码提供了编码和解码函数。 [**RFC 3548**](https://tools.ietf.org/html/rfc3548.html) \[https://tools.ietf.org/html/rfc3548.html\] 编码的目的是使得二进制数据可以作为电子邮件的内容正确地发送,用作 URL 的一部分,或者作为 HTTP POST 请求的一部分。其中的编码算法和 **uuencode** 程序是不同的。 此模块提供了两个接口。新的接口提供了从 [类字节对象](../glossary.xhtml#term-bytes-like-object) 到 ASCII 字节 [`bytes`](stdtypes.xhtml#bytes "bytes") 的编码,以及将 ASCII 的 [类字节对象](../glossary.xhtml#term-bytes-like-object) 或字符串解码到 [`bytes`](stdtypes.xhtml#bytes "bytes") 的操作。此模块支持定义在 [**RFC 3548**](https://tools.ietf.org/html/rfc3548.html) \[https://tools.ietf.org/html/rfc3548.html\] 中的所有 base-64 字母表 (普通的、URL 安全的和文件系统安全的)。 旧的接口不提供从字符串的解码操作,但提供了操作 [文件对象](../glossary.xhtml#term-file-object) 的编码和解码函数。旧接口只支持标准的 Base64 字母表,并且按照 [**RFC 2045**](https://tools.ietf.org/html/rfc2045.html) \[https://tools.ietf.org/html/rfc2045.html\] 的规范每 76 个字符增加一个换行符。注意:如果你需要支持 [**RFC 2045**](https://tools.ietf.org/html/rfc2045.html) \[https://tools.ietf.org/html/rfc2045.html\],那么使用 [`email`](email.xhtml#module-email "email: Package supporting the parsing, manipulating, and generating email messages.") 模块可能更加合适。 在 3.3 版更改: 新的接口提供的解码函数现在已经支持只包含 ASCII 的 Unicode 字符串。 在 3.4 版更改: 所有 [类字节对象](../glossary.xhtml#term-bytes-like-object) 现在已经被所有编码和解码函数接受。添加了对 Ascii85/Base85 的支持。 新的接口提供: `base64.``b64encode`(*s*, *altchars=None*)对 [bytes-like object](../glossary.xhtml#term-bytes-like-object) *s* 进行 Base64 编码,并返回编码后的 [`bytes`](stdtypes.xhtml#bytes "bytes")。 可选项 *altchars* 必须是一个长 2 字节的 [bytes-like object](../glossary.xhtml#term-bytes-like-object),它指定了用于替换 `+` 和 `/` 的字符。这允许应用程序生成 URL 或文件系统安全的 Base64 字符串。默认值是 `None`,使用标准 Base64 字母表。 `base64.``b64decode`(*s*, *altchars=None*, *validate=False*)解码 Base64 编码过的 [bytes-like object](../glossary.xhtml#term-bytes-like-object) 或 ASCII 字符串 *s* 并返回解码过的 [`bytes`](stdtypes.xhtml#bytes "bytes")。 可选项 *altchars* 必须是一个长 2 字节的 [bytes-like object](../glossary.xhtml#term-bytes-like-object),它指定了用于替换 `+` 和 `/` 的字符。 如果 *s* 被不正确地填写,一个 [`binascii.Error`](binascii.xhtml#binascii.Error "binascii.Error") 错误将被抛出。 如果 *validate* 值为 `False` (默认情况),则在填充检查前,将丢弃既不在标准 base-64 字母表之中也不在备用字母表中的字符。如果 *validate* 为 `True`,这些非 base64 字符将导致 [`binascii.Error`](binascii.xhtml#binascii.Error "binascii.Error")。 `base64.``standard_b64encode`(*s*)编码 [bytes-like object](../glossary.xhtml#term-bytes-like-object) *s*,使用标准 Base64 字母表并返回编码过的 [`bytes`](stdtypes.xhtml#bytes "bytes")。 `base64.``standard_b64decode`(*s*)解码 [bytes-like object](../glossary.xhtml#term-bytes-like-object) 或 ASCII 字符串 *s*,使用标准 Base64 字母表并返回编码过的 [`bytes`](stdtypes.xhtml#bytes "bytes")。 `base64.``urlsafe_b64encode`(*s*)编码 [bytes-like object](../glossary.xhtml#term-bytes-like-object) *s*,使用 URL 与文件系统安全的字母表,使用 `-` 以及 `_` 代替标准 Base64 字母表中的 `+` 和 `/`。返回编码过的 [`bytes`](stdtypes.xhtml#bytes "bytes")。结果中可能包含 `=`。 `base64.``urlsafe_b64decode`(*s*)解码 [bytes-like object](../glossary.xhtml#term-bytes-like-object) 或 ASCII 字符串 *s*,使用 URL 与文件系统安全的字母表,使用 `-` 以及 `_` 代替标准 Base64 字母表中的 `+` 和 `/`。返回解码过的 [`bytes`](stdtypes.xhtml#bytes "bytes") `base64.``b32encode`(*s*)用 Base32 编码 [bytes-like object](../glossary.xhtml#term-bytes-like-object) *s* 并返回编码过的 [`bytes`](stdtypes.xhtml#bytes "bytes") `base64.``b32decode`(*s*, *casefold=False*, *map01=None*)解码 Base32 编码过的 [bytes-like object](../glossary.xhtml#term-bytes-like-object) 或 ASCII 字符串 *s* 并返回解码过的 [`bytes`](stdtypes.xhtml#bytes "bytes")。 可选的 *casefold* 是一个指定小写字幕是否可接受为输入的标志。为了安全考虑,默认值为 `False`。 [**RFC 3548**](https://tools.ietf.org/html/rfc3548.html) \[https://tools.ietf.org/html/rfc3548.html\] 允许将字母 0(zero) 映射为字母 O(oh),并可以选择是否将字母 1(one) 映射为 I(eye) 或 L(el)。可选参数 *map01* 不是 `None` 时,它的值指定 1 的映射目标 (I 或 l),当 *map01* 非 `None` 时, 0 总是被映射为 O。为了安全考虑,默认值被设为 `None`,如果是这样, 0 和 1 不允许被作为输入。 如果 *s* 被错误地填写或输入中存在字母表之外的字符,将抛出 [`binascii.Error`](binascii.xhtml#binascii.Error "binascii.Error")。 `base64.``b16encode`(*s*)用 Base16 编码 [bytes-like object](../glossary.xhtml#term-bytes-like-object) *s* 并返回编码过的 [`bytes`](stdtypes.xhtml#bytes "bytes") `base64.``b16decode`(*s*, *casefold=False*)解码 Base16 编码过的 [bytes-like object](../glossary.xhtml#term-bytes-like-object) 或 ASCII 字符串 *s* 并返回解码过的 [`bytes`](stdtypes.xhtml#bytes "bytes")。 可选的 *casefold* 是一个指定小写字幕是否可接受为输入的标志。为了安全考虑,默认值为 `False`。 如果 *s* 被错误地填写或输入中存在字母表之外的字符,将抛出 [`binascii.Error`](binascii.xhtml#binascii.Error "binascii.Error")。 `base64.``a85encode`(*b*, *\**, *foldspaces=False*, *wrapcol=0*, *pad=False*, *adobe=False*)用 Ascii85 编码 [bytes-like object](../glossary.xhtml#term-bytes-like-object) *s* 并返回编码过的 [`bytes`](stdtypes.xhtml#bytes "bytes") *foldspaces* 是一个可选的标志,使用特殊的短序列 'y' 代替 'btoa' 提供的 4 个连续空格 (ASCII 0x20)。这个特性不被 "标准" Ascii85 编码支持。 *wrapcol* 控制了输出是否包含换行符 (`b'\n'`). 如果该值非零, 则每一行只有该值所限制的字符长度. *pad* 控制在编码之前输入是否填充为4的倍数。请注意,`btoa` 实现总是填充。 *adobe* controls whether the encoded byte sequence is framed with `<~`and `~>`, which is used by the Adobe implementation. 3\.4 新版功能. `base64.``a85decode`(*b*, *\**, *foldspaces=False*, *adobe=False*, *ignorechars=b' \\t\\n\\r\\v'*)解码 Ascii85 编码过的 [bytes-like object](../glossary.xhtml#term-bytes-like-object) 或 ASCII 字符串 *s* 并返回解码过的 [`bytes`](stdtypes.xhtml#bytes "bytes")。 *foldspaces* is a flag that specifies whether the 'y' short sequence should be accepted as shorthand for 4 consecutive spaces (ASCII 0x20). This feature is not supported by the "standard" Ascii85 encoding. *adobe* controls whether the input sequence is in Adobe Ascii85 format (i.e. is framed with <~ and ~>). *ignorechars* should be a [bytes-like object](../glossary.xhtml#term-bytes-like-object) or ASCII string containing characters to ignore from the input. This should only contain whitespace characters, and by default contains all whitespace characters in ASCII. 3\.4 新版功能. `base64.``b85encode`(*b*, *pad=False*)Encode the [bytes-like object](../glossary.xhtml#term-bytes-like-object) *b* using base85 (as used in e.g. git-style binary diffs) and return the encoded [`bytes`](stdtypes.xhtml#bytes "bytes"). If *pad* is true, the input is padded with `b'\0'` so its length is a multiple of 4 bytes before encoding. 3\.4 新版功能. `base64.``b85decode`(*b*)Decode the base85-encoded [bytes-like object](../glossary.xhtml#term-bytes-like-object) or ASCII string *b* and return the decoded [`bytes`](stdtypes.xhtml#bytes "bytes"). Padding is implicitly removed, if necessary. 3\.4 新版功能. The legacy interface: `base64.``decode`(*input*, *output*)Decode the contents of the binary *input* file and write the resulting binary data to the *output* file. *input* and *output* must be [file objects](../glossary.xhtml#term-file-object). *input* will be read until `input.readline()` returns an empty bytes object. `base64.``decodebytes`(*s*)Decode the [bytes-like object](../glossary.xhtml#term-bytes-like-object) *s*, which must contain one or more lines of base64 encoded data, and return the decoded [`bytes`](stdtypes.xhtml#bytes "bytes"). 3\.1 新版功能. `base64.``decodestring`(*s*)Deprecated alias of [`decodebytes()`](#base64.decodebytes "base64.decodebytes"). 3\.1 版后已移除. `base64.``encode`(*input*, *output*)Encode the contents of the binary *input* file and write the resulting base64 encoded data to the *output* file. *input* and *output* must be [file objects](../glossary.xhtml#term-file-object). *input* will be read until `input.read()` returns an empty bytes object. [`encode()`](#base64.encode "base64.encode") inserts a newline character (`b'\n'`) after every 76 bytes of the output, as well as ensuring that the output always ends with a newline, as per [**RFC 2045**](https://tools.ietf.org/html/rfc2045.html) \[https://tools.ietf.org/html/rfc2045.html\] (MIME). `base64.``encodebytes`(*s*)Encode the [bytes-like object](../glossary.xhtml#term-bytes-like-object) *s*, which can contain arbitrary binary data, and return [`bytes`](stdtypes.xhtml#bytes "bytes") containing the base64-encoded data, with newlines (`b'\n'`) inserted after every 76 bytes of output, and ensuring that there is a trailing newline, as per [**RFC 2045**](https://tools.ietf.org/html/rfc2045.html) \[https://tools.ietf.org/html/rfc2045.html\] (MIME). 3\.1 新版功能. `base64.``encodestring`(*s*)Deprecated alias of [`encodebytes()`](#base64.encodebytes "base64.encodebytes"). 3\.1 版后已移除. An example usage of the module: ``` >>> import base64 >>> encoded = base64.b64encode(b'data to be encoded') >>> encoded b'ZGF0YSB0byBiZSBlbmNvZGVk' >>> data = base64.b64decode(encoded) >>> data b'data to be encoded' ``` 参见 模块 [`binascii`](binascii.xhtml#module-binascii "binascii: Tools for converting between binary and various ASCII-encoded binary representations.")支持模块,包含ASCII到二进制和二进制到ASCII转换。 [**RFC 1521**](https://tools.ietf.org/html/rfc1521.html) \[https://tools.ietf.org/html/rfc1521.html\] - MIME (Multipurpose Internet Mail Extensions) Part One: Mechanisms for Specifying and Describing the Format of Internet Message BodiesSection 5.2, "Base64 Content-Transfer-Encoding," provides the definition of the base64 encoding. ### 导航 - [索引](../genindex.xhtml "总目录") - [模块](../py-modindex.xhtml "Python 模块索引") | - [下一页](binhex.xhtml "binhex --- 对binhex4文件进行编码和解码") | - [上一页](mimetypes.xhtml "mimetypes --- Map filenames to MIME types") | - ![](https://box.kancloud.cn/a721fc7ec672275e257bbbfde49a4d4e_16x16.png) - [Python](https://www.python.org/) » - zh\_CN 3.7.3 [文档](../index.xhtml) » - [Python 标准库](index.xhtml) » - [互联网数据处理](netdata.xhtml) » - $('.inline-search').show(0); | © [版权所有](../copyright.xhtml) 2001-2019, Python Software Foundation. Python 软件基金会是一个非盈利组织。 [请捐助。](https://www.python.org/psf/donations/) 最后更新于 5月 21, 2019. [发现了问题](../bugs.xhtml)? 使用[Sphinx](http://sphinx.pocoo.org/)1.8.4 创建。