701 lines
55 KiB
HTML
701 lines
55 KiB
HTML
|
||
<!DOCTYPE html>
|
||
|
||
<html xmlns="http://www.w3.org/1999/xhtml" lang="zh_CN">
|
||
<head>
|
||
<meta charset="utf-8" /><meta name="generator" content="Docutils 0.17.1: http://docutils.sourceforge.net/" />
|
||
|
||
<title>struct --- 将字节串解读为打包的二进制数据 — Python 3.8.20 文档</title><meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||
|
||
<link rel="stylesheet" href="../_static/pydoctheme.css" type="text/css" />
|
||
<link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
|
||
|
||
<script id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>
|
||
<script src="../_static/jquery.js"></script>
|
||
<script src="../_static/underscore.js"></script>
|
||
<script src="../_static/doctools.js"></script>
|
||
<script src="../_static/language_data.js"></script>
|
||
<script src="../_static/translations.js"></script>
|
||
|
||
<script src="../_static/sidebar.js"></script>
|
||
|
||
<link rel="search" type="application/opensearchdescription+xml"
|
||
title="在 Python 3.8.20 文档 中搜索"
|
||
href="../_static/opensearch.xml"/>
|
||
<link rel="author" title="关于这些文档" href="../about.html" />
|
||
<link rel="index" title="索引" href="../genindex.html" />
|
||
<link rel="search" title="搜索" href="../search.html" />
|
||
<link rel="copyright" title="版权所有" href="../copyright.html" />
|
||
<link rel="next" title="codecs --- 编解码器注册和相关基类" href="codecs.html" />
|
||
<link rel="prev" title="二进制数据服务" href="binary.html" />
|
||
<link rel="canonical" href="https://docs.python.org/3/library/struct.html" />
|
||
|
||
|
||
|
||
|
||
|
||
<style>
|
||
@media only screen {
|
||
table.full-width-table {
|
||
width: 100%;
|
||
}
|
||
}
|
||
</style>
|
||
<link rel="shortcut icon" type="image/png" href="../_static/py.svg" />
|
||
<script type="text/javascript" src="../_static/copybutton.js"></script>
|
||
<script type="text/javascript" src="../_static/menu.js"></script>
|
||
|
||
</head>
|
||
<body>
|
||
<div class="mobile-nav">
|
||
<input type="checkbox" id="menuToggler" class="toggler__input" aria-controls="navigation"
|
||
aria-pressed="false" aria-expanded="false" role="button" aria-label="Menu" />
|
||
<label for="menuToggler" class="toggler__label">
|
||
<span></span>
|
||
</label>
|
||
<nav class="nav-content" role="navigation">
|
||
<a href="https://www.python.org/" class="nav-logo">
|
||
<img src="../_static/py.svg" alt="Logo"/>
|
||
</a>
|
||
<div class="version_switcher_placeholder"></div>
|
||
<form role="search" class="search" action="../search.html" method="get">
|
||
<svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" class="search-icon">
|
||
<path fill-rule="nonzero"
|
||
d="M15.5 14h-.79l-.28-.27a6.5 6.5 0 001.48-5.34c-.47-2.78-2.79-5-5.59-5.34a6.505 6.505 0 00-7.27 7.27c.34 2.8 2.56 5.12 5.34 5.59a6.5 6.5 0 005.34-1.48l.27.28v.79l4.25 4.25c.41.41 1.08.41 1.49 0 .41-.41.41-1.08 0-1.49L15.5 14zm-6 0C7.01 14 5 11.99 5 9.5S7.01 5 9.5 5 14 7.01 14 9.5 11.99 14 9.5 14z" fill="#444"></path>
|
||
</svg>
|
||
<input type="text" name="q" aria-label="快速搜索"/>
|
||
<input type="submit" value="转向"/>
|
||
</form>
|
||
</nav>
|
||
<div class="menu-wrapper">
|
||
<nav class="menu" role="navigation" aria-label="main navigation">
|
||
<div class="language_switcher_placeholder"></div>
|
||
<h3><a href="../contents.html">目录</a></h3>
|
||
<ul>
|
||
<li><a class="reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">struct</span></code> --- 将字节串解读为打包的二进制数据</a><ul>
|
||
<li><a class="reference internal" href="#functions-and-exceptions">函数和异常</a></li>
|
||
<li><a class="reference internal" href="#format-strings">格式字符串</a><ul>
|
||
<li><a class="reference internal" href="#byte-order-size-and-alignment">字节顺序,大小和对齐方式</a></li>
|
||
<li><a class="reference internal" href="#format-characters">格式字符</a></li>
|
||
<li><a class="reference internal" href="#examples">例子</a></li>
|
||
</ul>
|
||
</li>
|
||
<li><a class="reference internal" href="#classes">类</a></li>
|
||
</ul>
|
||
</li>
|
||
</ul>
|
||
|
||
<h4>上一个主题</h4>
|
||
<p class="topless"><a href="binary.html"
|
||
title="上一章">二进制数据服务</a></p>
|
||
<h4>下一个主题</h4>
|
||
<p class="topless"><a href="codecs.html"
|
||
title="下一章"><code class="xref py py-mod docutils literal notranslate"><span class="pre">codecs</span></code> --- 编解码器注册和相关基类</a></p>
|
||
<div role="note" aria-label="source link">
|
||
<h3>本页</h3>
|
||
<ul class="this-page-menu">
|
||
<li><a href="../bugs.html">报告 Bug</a></li>
|
||
<li>
|
||
<a href="https://github.com/python/cpython/blob/3.8/Doc/library/struct.rst"
|
||
rel="nofollow">显示源代码
|
||
</a>
|
||
</li>
|
||
</ul>
|
||
</div>
|
||
</nav>
|
||
</div>
|
||
</div>
|
||
|
||
<div id="outdated-warning" style="padding: .5em; text-align: center; background-color: #FFBABA; color: #6A0E0E;">
|
||
这个文档所针对的是一个已不再受支持的 Python 旧版本。
|
||
你应当升级版本,并阅读
|
||
<a href="/3/library/struct.html"> Python 当前稳定版本的文档</a>.
|
||
</div>
|
||
|
||
<div class="related" role="navigation" aria-label="related navigation">
|
||
<h3>导航</h3>
|
||
<ul>
|
||
<li class="right" style="margin-right: 10px">
|
||
<a href="../genindex.html" title="总目录"
|
||
accesskey="I">索引</a></li>
|
||
<li class="right" >
|
||
<a href="../py-modindex.html" title="Python 模块索引"
|
||
>模块</a> |</li>
|
||
<li class="right" >
|
||
<a href="codecs.html" title="codecs --- 编解码器注册和相关基类"
|
||
accesskey="N">下一页</a> |</li>
|
||
<li class="right" >
|
||
<a href="binary.html" title="二进制数据服务"
|
||
accesskey="P">上一页</a> |</li>
|
||
|
||
<li><img src="../_static/py.svg" alt="python logo" style="vertical-align: middle; margin-top: -1px"/></li>
|
||
<li><a href="https://www.python.org/">Python</a> »</li>
|
||
<li class="switchers">
|
||
<div class="language_switcher_placeholder"></div>
|
||
<div class="version_switcher_placeholder"></div>
|
||
</li>
|
||
<li>
|
||
|
||
</li>
|
||
<li id="cpython-language-and-version">
|
||
<a href="../index.html">3.8.20 Documentation</a> »
|
||
</li>
|
||
|
||
<li class="nav-item nav-item-1"><a href="index.html" >Python 标准库</a> »</li>
|
||
<li class="nav-item nav-item-2"><a href="binary.html" accesskey="U">二进制数据服务</a> »</li>
|
||
<li class="right">
|
||
|
||
|
||
<div class="inline-search" role="search">
|
||
<form class="inline-search" action="../search.html" method="get">
|
||
<input placeholder="快速搜索" aria-label="快速搜索" type="text" name="q" />
|
||
<input type="submit" value="转向" />
|
||
<input type="hidden" name="check_keywords" value="yes" />
|
||
<input type="hidden" name="area" value="default" />
|
||
</form>
|
||
</div>
|
||
|
|
||
</li>
|
||
|
||
</ul>
|
||
</div>
|
||
|
||
<div class="document">
|
||
<div class="documentwrapper">
|
||
<div class="bodywrapper">
|
||
<div class="body" role="main">
|
||
|
||
<section id="module-struct">
|
||
<span id="struct-interpret-bytes-as-packed-binary-data"></span><h1><a class="reference internal" href="#module-struct" title="struct: Interpret bytes as packed binary data."><code class="xref py py-mod docutils literal notranslate"><span class="pre">struct</span></code></a> --- 将字节串解读为打包的二进制数据<a class="headerlink" href="#module-struct" title="永久链接至标题">¶</a></h1>
|
||
<p><strong>源代码:</strong> <a class="reference external" href="https://github.com/python/cpython/tree/3.8/Lib/struct.py">Lib/struct.py</a></p>
|
||
<hr class="docutils" id="index-0" />
|
||
<p>此模块可以执行 Python 值和以 Python <a class="reference internal" href="stdtypes.html#bytes" title="bytes"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code></a> 对象表示的 C 结构之间的转换。 这可以被用来处理存储在文件中或是从网络连接等其他来源获取的二进制数据。 它使用 <a class="reference internal" href="#struct-format-strings"><span class="std std-ref">格式字符串</span></a> 作为 C 结构布局的精简描述以及与 Python 值的双向转换。</p>
|
||
<div class="admonition note">
|
||
<p class="admonition-title">注解</p>
|
||
<p>默认情况下,打包给定 C 结构的结果会包含填充字节以使得所涉及的 C 类型保持正确的对齐;类似地,对齐在解包时也会被纳入考虑。 选择此种行为的目的是使得被打包结构的字节能与相应 C 结构在内存中的布局完全一致。 要处理平台独立的数据格式或省略隐式的填充字节,请使用 <code class="docutils literal notranslate"><span class="pre">standard</span></code> 大小和对齐而不是 <code class="docutils literal notranslate"><span class="pre">native</span></code> 大小和对齐:详情参见 <a class="reference internal" href="#struct-alignment"><span class="std std-ref">字节顺序,大小和对齐方式</span></a>。</p>
|
||
</div>
|
||
<p>某些 <a class="reference internal" href="#module-struct" title="struct: Interpret bytes as packed binary data."><code class="xref py py-mod docutils literal notranslate"><span class="pre">struct</span></code></a> 的函数(以及 <a class="reference internal" href="#struct.Struct" title="struct.Struct"><code class="xref py py-class docutils literal notranslate"><span class="pre">Struct</span></code></a> 的方法)接受一个 <em>buffer</em> 参数。 这将指向实现了 <a class="reference internal" href="../c-api/buffer.html#bufferobjects"><span class="std std-ref">缓冲协议</span></a> 并提供只读或是可读写缓冲的对象。 用于此目的的最常见类型为 <a class="reference internal" href="stdtypes.html#bytes" title="bytes"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code></a> 和 <a class="reference internal" href="stdtypes.html#bytearray" title="bytearray"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytearray</span></code></a>,但许多其他可被视为字节数组的类型也实现了缓冲协议,因此它们无需额外从 <a class="reference internal" href="stdtypes.html#bytes" title="bytes"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code></a> 对象复制即可被读取或填充。</p>
|
||
<section id="functions-and-exceptions">
|
||
<h2>函数和异常<a class="headerlink" href="#functions-and-exceptions" title="永久链接至标题">¶</a></h2>
|
||
<p>此模块定义了下列异常和函数:</p>
|
||
<dl class="exception">
|
||
<dt id="struct.error">
|
||
<em class="property">exception </em><code class="sig-prename descclassname">struct.</code><code class="sig-name descname">error</code><a class="headerlink" href="#struct.error" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>会在多种场合下被引发的异常;其参数为一个描述错误信息的字符串。</p>
|
||
</dd></dl>
|
||
|
||
<dl class="function">
|
||
<dt id="struct.pack">
|
||
<code class="sig-prename descclassname">struct.</code><code class="sig-name descname">pack</code><span class="sig-paren">(</span><em class="sig-param">format</em>, <em class="sig-param">v1</em>, <em class="sig-param">v2</em>, <em class="sig-param">...</em><span class="sig-paren">)</span><a class="headerlink" href="#struct.pack" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>返回一个 bytes 对象,其中包含根据格式字符串 <em>format</em> 打包的值 <em>v1</em>, <em>v2</em>, ... 参数个数必须与格式字符串所要求的值完全匹配。</p>
|
||
</dd></dl>
|
||
|
||
<dl class="function">
|
||
<dt id="struct.pack_into">
|
||
<code class="sig-prename descclassname">struct.</code><code class="sig-name descname">pack_into</code><span class="sig-paren">(</span><em class="sig-param">format</em>, <em class="sig-param">buffer</em>, <em class="sig-param">offset</em>, <em class="sig-param">v1</em>, <em class="sig-param">v2</em>, <em class="sig-param">...</em><span class="sig-paren">)</span><a class="headerlink" href="#struct.pack_into" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>根据格式字符串 <em>format</em> 打包 <em>v1</em>, <em>v2</em>, ... 等值并将打包的字节串写入可写缓冲区 <em>buffer</em> 从 <em>offset</em> 开始的位置。 请注意 <em>offset</em> 是必需的参数。</p>
|
||
</dd></dl>
|
||
|
||
<dl class="function">
|
||
<dt id="struct.unpack">
|
||
<code class="sig-prename descclassname">struct.</code><code class="sig-name descname">unpack</code><span class="sig-paren">(</span><em class="sig-param">format</em>, <em class="sig-param">buffer</em><span class="sig-paren">)</span><a class="headerlink" href="#struct.unpack" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>根据格式字符串 <em>format</em> 从缓冲区 <em>buffer</em> 解包(假定是由 <code class="docutils literal notranslate"><span class="pre">pack(format,</span> <span class="pre">...)</span></code> 打包)。 结果为一个元组,即使其只包含一个条目。 缓冲区的字节大小必须匹配格式所要求的大小,如 <a class="reference internal" href="#struct.calcsize" title="struct.calcsize"><code class="xref py py-func docutils literal notranslate"><span class="pre">calcsize()</span></code></a> 所示。</p>
|
||
</dd></dl>
|
||
|
||
<dl class="function">
|
||
<dt id="struct.unpack_from">
|
||
<code class="sig-prename descclassname">struct.</code><code class="sig-name descname">unpack_from</code><span class="sig-paren">(</span><em class="sig-param">format</em>, <em class="sig-param">buffer</em>, <em class="sig-param">offset=0</em><span class="sig-paren">)</span><a class="headerlink" href="#struct.unpack_from" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>对 <em>buffer</em> 从位置 <em>offset</em> 开始根据格式字符串 <em>format</em> 进行解包。 结果为一个元组,即使其中只包含一个条目。 缓冲区的字节大小从位置 <em>offset</em> 开始必须至少为 <a class="reference internal" href="#struct.calcsize" title="struct.calcsize"><code class="xref py py-func docutils literal notranslate"><span class="pre">calcsize()</span></code></a> 显示的格式所要求的大小。</p>
|
||
</dd></dl>
|
||
|
||
<dl class="function">
|
||
<dt id="struct.iter_unpack">
|
||
<code class="sig-prename descclassname">struct.</code><code class="sig-name descname">iter_unpack</code><span class="sig-paren">(</span><em class="sig-param">format</em>, <em class="sig-param">buffer</em><span class="sig-paren">)</span><a class="headerlink" href="#struct.iter_unpack" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>根据格式字符串 <em>format</em> 以迭代方式从缓冲区 <em>buffer</em> 解包。 此函数返回一个迭代器,它将从缓冲区读取相同大小的块直至其内容全部耗尽。 缓冲区的字节大小必须整数倍于格式所要求的大小,如 <a class="reference internal" href="#struct.calcsize" title="struct.calcsize"><code class="xref py py-func docutils literal notranslate"><span class="pre">calcsize()</span></code></a> 所示。</p>
|
||
<p>每次迭代将产生一个如格式字符串所指定的元组。</p>
|
||
<div class="versionadded">
|
||
<p><span class="versionmodified added">3.4 新版功能.</span></p>
|
||
</div>
|
||
</dd></dl>
|
||
|
||
<dl class="function">
|
||
<dt id="struct.calcsize">
|
||
<code class="sig-prename descclassname">struct.</code><code class="sig-name descname">calcsize</code><span class="sig-paren">(</span><em class="sig-param">format</em><span class="sig-paren">)</span><a class="headerlink" href="#struct.calcsize" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>返回与格式字符串 <em>format</em> 相对应的结构的大小(亦即 <code class="docutils literal notranslate"><span class="pre">pack(format,</span> <span class="pre">...)</span></code> 所产生的字节串对象的大小)。</p>
|
||
</dd></dl>
|
||
|
||
</section>
|
||
<section id="format-strings">
|
||
<span id="struct-format-strings"></span><h2>格式字符串<a class="headerlink" href="#format-strings" title="永久链接至标题">¶</a></h2>
|
||
<p>格式字符串是用来在打包和解包数据时指定预期布局的机制。 它们使用指定被打包/解包数据类型的 <a class="reference internal" href="#format-characters"><span class="std std-ref">格式字符</span></a> 进行构建。 此外,还有一些特殊字符用来控制 <a class="reference internal" href="#struct-alignment"><span class="std std-ref">字节顺序,大小和对齐方式</span></a>。</p>
|
||
<section id="byte-order-size-and-alignment">
|
||
<span id="struct-alignment"></span><h3>字节顺序,大小和对齐方式<a class="headerlink" href="#byte-order-size-and-alignment" title="永久链接至标题">¶</a></h3>
|
||
<p>默认情况下,C类型以机器的本机格式和字节顺序表示,并在必要时通过跳过填充字节进行正确对齐(根据C编译器使用的规则)。</p>
|
||
<p id="index-1">或者,根据下表,格式字符串的第一个字符可用于指示打包数据的字节顺序,大小和对齐方式:</p>
|
||
<table class="docutils align-default">
|
||
<colgroup>
|
||
<col style="width: 20%" />
|
||
<col style="width: 43%" />
|
||
<col style="width: 18%" />
|
||
<col style="width: 20%" />
|
||
</colgroup>
|
||
<thead>
|
||
<tr class="row-odd"><th class="head"><p>字符</p></th>
|
||
<th class="head"><p>字节顺序</p></th>
|
||
<th class="head"><p>大小</p></th>
|
||
<th class="head"><p>对齐方式</p></th>
|
||
</tr>
|
||
</thead>
|
||
<tbody>
|
||
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">@</span></code></p></td>
|
||
<td><p>按原字节</p></td>
|
||
<td><p>按原字节</p></td>
|
||
<td><p>按原字节</p></td>
|
||
</tr>
|
||
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">=</span></code></p></td>
|
||
<td><p>按原字节</p></td>
|
||
<td><p>标准</p></td>
|
||
<td><p>无</p></td>
|
||
</tr>
|
||
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre"><</span></code></p></td>
|
||
<td><p>小端</p></td>
|
||
<td><p>标准</p></td>
|
||
<td><p>无</p></td>
|
||
</tr>
|
||
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">></span></code></p></td>
|
||
<td><p>大端</p></td>
|
||
<td><p>标准</p></td>
|
||
<td><p>无</p></td>
|
||
</tr>
|
||
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">!</span></code></p></td>
|
||
<td><p>网络(=大端)</p></td>
|
||
<td><p>标准</p></td>
|
||
<td><p>无</p></td>
|
||
</tr>
|
||
</tbody>
|
||
</table>
|
||
<p>如果第一个字符不是其中之一,则假定为 <code class="docutils literal notranslate"><span class="pre">'@'</span></code> 。</p>
|
||
<p>本机字节顺序可能为大端或是小端,取决于主机系统的不同。 例如, Intel x86 和 AMD64 (x86-64) 是小端的;Motorola 68000 和 PowerPC G5 是大端的;ARM 和 Intel Itanium 具有可切换的字节顺序(双端)。 请使用 <code class="docutils literal notranslate"><span class="pre">sys.byteorder</span></code> 来检查你的系统字节顺序。</p>
|
||
<p>本机大小和对齐方式是使用 C 编译器的 <code class="docutils literal notranslate"><span class="pre">sizeof</span></code> 表达式来确定的。 这总是会与本机字节顺序相绑定。</p>
|
||
<p>标准大小仅取决于格式字符;请参阅 <a class="reference internal" href="#format-characters"><span class="std std-ref">格式字符</span></a> 部分中的表格。</p>
|
||
<p>请注意 <code class="docutils literal notranslate"><span class="pre">'@'</span></code> 和 <code class="docutils literal notranslate"><span class="pre">'='</span></code> 之间的区别:两个都使用本机字节顺序,但后者的大小和对齐方式是标准化的。</p>
|
||
<p>形式 <code class="docutils literal notranslate"><span class="pre">'!'</span></code> 代表网络字节顺序总是使用在 <a class="reference external" href="https://tools.ietf.org/html/rfc1700">IETF RFC 1700</a> 中所定义的大端序。</p>
|
||
<p>没有什么方式能指定非本机字节顺序(强制字节对调);请正确选择使用 <code class="docutils literal notranslate"><span class="pre">'<'</span></code> 或 <code class="docutils literal notranslate"><span class="pre">'>'</span></code>。</p>
|
||
<p>注释:</p>
|
||
<ol class="arabic simple">
|
||
<li><p>填充只会在连续结构成员之间自动添加。 填充不会添加到已编码结构的开头和末尾。</p></li>
|
||
<li><p>当使用非本机大小和对齐方式即 '<', '>', '=', and '!' 时不会添加任何填充。</p></li>
|
||
<li><p>要将结构的末尾对齐到符合特定类型的对齐要求,请以该类型代码加重复计数的零作为格式结束。 参见 <a class="reference internal" href="#struct-examples"><span class="std std-ref">例子</span></a>。</p></li>
|
||
</ol>
|
||
</section>
|
||
<section id="format-characters">
|
||
<span id="id1"></span><h3>格式字符<a class="headerlink" href="#format-characters" title="永久链接至标题">¶</a></h3>
|
||
<p>格式字符具有以下含义;C 和 Python 值之间的按其指定类型的转换应当是相当明显的。 ‘标准大小’列是指当使用标准大小时以字节表示的已打包值大小;也就是当格式字符串以 <code class="docutils literal notranslate"><span class="pre">'<'</span></code>, <code class="docutils literal notranslate"><span class="pre">'>'</span></code>, <code class="docutils literal notranslate"><span class="pre">'!'</span></code> 或 <code class="docutils literal notranslate"><span class="pre">'='</span></code> 之一开头的情况。 当使用本机大小时,已打包值的大小取决于具体的平台。</p>
|
||
<table class="docutils align-default">
|
||
<colgroup>
|
||
<col style="width: 10%" />
|
||
<col style="width: 32%" />
|
||
<col style="width: 24%" />
|
||
<col style="width: 20%" />
|
||
<col style="width: 15%" />
|
||
</colgroup>
|
||
<thead>
|
||
<tr class="row-odd"><th class="head"><p>格式</p></th>
|
||
<th class="head"><p>C 类型</p></th>
|
||
<th class="head"><p>Python 类型</p></th>
|
||
<th class="head"><p>标准大小</p></th>
|
||
<th class="head"><p>备注</p></th>
|
||
</tr>
|
||
</thead>
|
||
<tbody>
|
||
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">x</span></code></p></td>
|
||
<td><p>填充字节</p></td>
|
||
<td><p>无</p></td>
|
||
<td></td>
|
||
<td></td>
|
||
</tr>
|
||
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">c</span></code></p></td>
|
||
<td><p><code class="xref c c-type docutils literal notranslate"><span class="pre">char</span></code></p></td>
|
||
<td><p>长度为 1 的字节串</p></td>
|
||
<td><p>1</p></td>
|
||
<td></td>
|
||
</tr>
|
||
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">b</span></code></p></td>
|
||
<td><p><code class="xref c c-type docutils literal notranslate"><span class="pre">signed</span> <span class="pre">char</span></code></p></td>
|
||
<td><p>integer</p></td>
|
||
<td><p>1</p></td>
|
||
<td><p>(1), (2)</p></td>
|
||
</tr>
|
||
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">B</span></code></p></td>
|
||
<td><p><code class="xref c c-type docutils literal notranslate"><span class="pre">unsigned</span> <span class="pre">char</span></code></p></td>
|
||
<td><p>integer</p></td>
|
||
<td><p>1</p></td>
|
||
<td><p>(2)</p></td>
|
||
</tr>
|
||
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">?</span></code></p></td>
|
||
<td><p><code class="xref c c-type docutils literal notranslate"><span class="pre">_Bool</span></code></p></td>
|
||
<td><p>bool</p></td>
|
||
<td><p>1</p></td>
|
||
<td><p>(1)</p></td>
|
||
</tr>
|
||
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">h</span></code></p></td>
|
||
<td><p><code class="xref c c-type docutils literal notranslate"><span class="pre">short</span></code></p></td>
|
||
<td><p>integer</p></td>
|
||
<td><p>2</p></td>
|
||
<td><p>(2)</p></td>
|
||
</tr>
|
||
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">H</span></code></p></td>
|
||
<td><p><code class="xref c c-type docutils literal notranslate"><span class="pre">unsigned</span> <span class="pre">short</span></code></p></td>
|
||
<td><p>integer</p></td>
|
||
<td><p>2</p></td>
|
||
<td><p>(2)</p></td>
|
||
</tr>
|
||
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">i</span></code></p></td>
|
||
<td><p><code class="xref c c-type docutils literal notranslate"><span class="pre">int</span></code></p></td>
|
||
<td><p>integer</p></td>
|
||
<td><p>4</p></td>
|
||
<td><p>(2)</p></td>
|
||
</tr>
|
||
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">I</span></code></p></td>
|
||
<td><p><code class="xref c c-type docutils literal notranslate"><span class="pre">unsigned</span> <span class="pre">int</span></code></p></td>
|
||
<td><p>integer</p></td>
|
||
<td><p>4</p></td>
|
||
<td><p>(2)</p></td>
|
||
</tr>
|
||
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">l</span></code></p></td>
|
||
<td><p><code class="xref c c-type docutils literal notranslate"><span class="pre">long</span></code></p></td>
|
||
<td><p>integer</p></td>
|
||
<td><p>4</p></td>
|
||
<td><p>(2)</p></td>
|
||
</tr>
|
||
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">L</span></code></p></td>
|
||
<td><p><code class="xref c c-type docutils literal notranslate"><span class="pre">unsigned</span> <span class="pre">long</span></code></p></td>
|
||
<td><p>integer</p></td>
|
||
<td><p>4</p></td>
|
||
<td><p>(2)</p></td>
|
||
</tr>
|
||
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">q</span></code></p></td>
|
||
<td><p><code class="xref c c-type docutils literal notranslate"><span class="pre">long</span> <span class="pre">long</span></code></p></td>
|
||
<td><p>integer</p></td>
|
||
<td><p>8</p></td>
|
||
<td><p>(2)</p></td>
|
||
</tr>
|
||
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">Q</span></code></p></td>
|
||
<td><p><code class="xref c c-type docutils literal notranslate"><span class="pre">unsigned</span> <span class="pre">long</span>
|
||
<span class="pre">long</span></code></p></td>
|
||
<td><p>integer</p></td>
|
||
<td><p>8</p></td>
|
||
<td><p>(2)</p></td>
|
||
</tr>
|
||
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">n</span></code></p></td>
|
||
<td><p><code class="xref c c-type docutils literal notranslate"><span class="pre">ssize_t</span></code></p></td>
|
||
<td><p>integer</p></td>
|
||
<td></td>
|
||
<td><p>(3)</p></td>
|
||
</tr>
|
||
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">N</span></code></p></td>
|
||
<td><p><code class="xref c c-type docutils literal notranslate"><span class="pre">size_t</span></code></p></td>
|
||
<td><p>integer</p></td>
|
||
<td></td>
|
||
<td><p>(3)</p></td>
|
||
</tr>
|
||
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">e</span></code></p></td>
|
||
<td><p>(6)</p></td>
|
||
<td><p>float</p></td>
|
||
<td><p>2</p></td>
|
||
<td><p>(4)</p></td>
|
||
</tr>
|
||
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">f</span></code></p></td>
|
||
<td><p><code class="xref c c-type docutils literal notranslate"><span class="pre">float</span></code></p></td>
|
||
<td><p>float</p></td>
|
||
<td><p>4</p></td>
|
||
<td><p>(4)</p></td>
|
||
</tr>
|
||
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">d</span></code></p></td>
|
||
<td><p><code class="xref c c-type docutils literal notranslate"><span class="pre">double</span></code></p></td>
|
||
<td><p>float</p></td>
|
||
<td><p>8</p></td>
|
||
<td><p>(4)</p></td>
|
||
</tr>
|
||
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">s</span></code></p></td>
|
||
<td><p><code class="xref c c-type docutils literal notranslate"><span class="pre">char[]</span></code></p></td>
|
||
<td><p>字节串</p></td>
|
||
<td></td>
|
||
<td></td>
|
||
</tr>
|
||
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">p</span></code></p></td>
|
||
<td><p><code class="xref c c-type docutils literal notranslate"><span class="pre">char[]</span></code></p></td>
|
||
<td><p>字节串</p></td>
|
||
<td></td>
|
||
<td></td>
|
||
</tr>
|
||
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">P</span></code></p></td>
|
||
<td><p><code class="xref c c-type docutils literal notranslate"><span class="pre">void</span> <span class="pre">*</span></code></p></td>
|
||
<td><p>integer</p></td>
|
||
<td></td>
|
||
<td><p>(5)</p></td>
|
||
</tr>
|
||
</tbody>
|
||
</table>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">在 3.3 版更改: </span>增加了对 <code class="docutils literal notranslate"><span class="pre">'n'</span></code> 和 <code class="docutils literal notranslate"><span class="pre">'N'</span></code> 格式的支持</p>
|
||
</div>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">在 3.6 版更改: </span>添加了对 <code class="docutils literal notranslate"><span class="pre">'e'</span></code> 格式的支持。</p>
|
||
</div>
|
||
<p>注释:</p>
|
||
<ol class="arabic">
|
||
<li><p id="index-2"><code class="docutils literal notranslate"><span class="pre">'?'</span></code> 转换码对应于 C99 定义的 <code class="xref c c-type docutils literal notranslate"><span class="pre">_Bool</span></code> 类型。 如果此类型不可用,则使用 <code class="xref c c-type docutils literal notranslate"><span class="pre">char</span></code> 来模拟。 在标准模式下,它总是以一个字节表示。</p>
|
||
</li>
|
||
<li><p>当尝试使用任何整数转换码打包一个非整数时,如果该非整数具有 <a class="reference internal" href="../reference/datamodel.html#object.__index__" title="object.__index__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__index__()</span></code></a> 方法,则会在打包之前调用该方法将参数转换为一个整数。</p>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">在 3.2 版更改: </span>为非整数使用 <a class="reference internal" href="../reference/datamodel.html#object.__index__" title="object.__index__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__index__()</span></code></a> 方法是 3.2 版的新增特性。</p>
|
||
</div>
|
||
</li>
|
||
<li><p><code class="docutils literal notranslate"><span class="pre">'n'</span></code> 和 <code class="docutils literal notranslate"><span class="pre">'N'</span></code> 转换码仅对本机大小可用(选择为默认或使用 <code class="docutils literal notranslate"><span class="pre">'@'</span></code> 字节顺序字符)。 对于标准大小,你可以使用适合你的应用的任何其他整数格式。</p></li>
|
||
<li><p>对于 <code class="docutils literal notranslate"><span class="pre">'f'</span></code>, <code class="docutils literal notranslate"><span class="pre">'d'</span></code> 和 <code class="docutils literal notranslate"><span class="pre">'e'</span></code> 转换码,打包表示形式将使用 IEEE 754 binary32, binary64 或 binary16 格式 (分别对应于 <code class="docutils literal notranslate"><span class="pre">'f'</span></code>, <code class="docutils literal notranslate"><span class="pre">'d'</span></code> 或 <code class="docutils literal notranslate"><span class="pre">'e'</span></code>),无论平台使用何种浮点格式。</p></li>
|
||
<li><p><code class="docutils literal notranslate"><span class="pre">'P'</span></code> 格式字符仅对本机字节顺序可用(选择为默认或使用 <code class="docutils literal notranslate"><span class="pre">'@'</span></code> 字节顺序字符)。 字节顺序字符 <code class="docutils literal notranslate"><span class="pre">'='</span></code> 选择使用基于主机系统的小端或大端排序。 struct 模块不会将其解读为本机排序,因此 <code class="docutils literal notranslate"><span class="pre">'P'</span></code> 格式将不可用。</p></li>
|
||
<li><p>IEEE 754 binary16 "半精度" 类型是在 <a class="reference external" href="https://en.wikipedia.org/wiki/IEEE_floating_point#IEEE_754-2008">IEEE 754 标准</a> 的 2008 修订版中引入的。 它包含一个符号位,5 个指数位和 11 个精度位(明确存储 10 位),可以完全精确地表示大致范围在 <code class="docutils literal notranslate"><span class="pre">6.1e-05</span></code> 和 <code class="docutils literal notranslate"><span class="pre">6.5e+04</span></code> 之间的数字。 此类型并不被 C 编译器广泛支持:在一台典型的机器上,可以使用 unsigned short 进行存储,但不会被用于数学运算。 请参阅维基百科页面 <a class="reference external" href="https://en.wikipedia.org/wiki/Half-precision_floating-point_format">half-precision floating-point format</a> 了解详情。</p></li>
|
||
</ol>
|
||
<p>格式字符之前可以带有整数重复计数。 例如,格式字符串 <code class="docutils literal notranslate"><span class="pre">'4h'</span></code> 的含义与 <code class="docutils literal notranslate"><span class="pre">'hhhh'</span></code> 完全相同。</p>
|
||
<p>格式之间的空白字符会被忽略;但是计数及其格式字符中不可有空白字符。</p>
|
||
<p>对于 <code class="docutils literal notranslate"><span class="pre">'s'</span></code> 格式字符,计数会被解析为字节的长度,而不是像其他格式字符那样的重复计数;例如,<code class="docutils literal notranslate"><span class="pre">'10s'</span></code> 表示一个 10 字节的字节串,而 <code class="docutils literal notranslate"><span class="pre">'10c'</span></code> 表示 10 个字符。 如果未给出计数,则默认值为 1。 对于打包操作,字节串会被适当地截断或填充空字节以符合要求。 对于解包操作,结果字节对象总是恰好具有指定数量的字节。 作为特殊情况,<code class="docutils literal notranslate"><span class="pre">'0s'</span></code> 表示一个空字符串(而 <code class="docutils literal notranslate"><span class="pre">'0c'</span></code> 表示 0 个字符)。</p>
|
||
<p>当使用某一种整数格式 (<code class="docutils literal notranslate"><span class="pre">'b'</span></code>, <code class="docutils literal notranslate"><span class="pre">'B'</span></code>, <code class="docutils literal notranslate"><span class="pre">'h'</span></code>, <code class="docutils literal notranslate"><span class="pre">'H'</span></code>, <code class="docutils literal notranslate"><span class="pre">'i'</span></code>, <code class="docutils literal notranslate"><span class="pre">'I'</span></code>, <code class="docutils literal notranslate"><span class="pre">'l'</span></code>, <code class="docutils literal notranslate"><span class="pre">'L'</span></code>, <code class="docutils literal notranslate"><span class="pre">'q'</span></code>, <code class="docutils literal notranslate"><span class="pre">'Q'</span></code>) 打包值 <code class="docutils literal notranslate"><span class="pre">x</span></code> 时,如果 <code class="docutils literal notranslate"><span class="pre">x</span></code> 在该格式的有效范围之外则将引发 <a class="reference internal" href="#struct.error" title="struct.error"><code class="xref py py-exc docutils literal notranslate"><span class="pre">struct.error</span></code></a>。</p>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">在 3.1 版更改: </span>在 3.0 中,某些包装了超范围值的整数格式会引发 <a class="reference internal" href="exceptions.html#DeprecationWarning" title="DeprecationWarning"><code class="xref py py-exc docutils literal notranslate"><span class="pre">DeprecationWarning</span></code></a> 而不是 <a class="reference internal" href="#struct.error" title="struct.error"><code class="xref py py-exc docutils literal notranslate"><span class="pre">struct.error</span></code></a>。</p>
|
||
</div>
|
||
<p><code class="docutils literal notranslate"><span class="pre">'p'</span></code> 格式字符用于编码“Pascal 字符串”,即存储在由计数指定的 <em>固定长度字节</em> 中的可变长度短字符串。 所存储的第一个字节为字符串长度或 255 中的较小值。 之后是字符串对应的字节。 如果传入 <a class="reference internal" href="#struct.pack" title="struct.pack"><code class="xref py py-func docutils literal notranslate"><span class="pre">pack()</span></code></a> 的字符串过长(超过计数值减 1),则只有字符串前 <code class="docutils literal notranslate"><span class="pre">count-1</span></code> 个字节会被存储。 如果字符串短于 <code class="docutils literal notranslate"><span class="pre">count-1</span></code>,则会填充空字节以使得恰好使用了 count 个字节。 请注意对于 <a class="reference internal" href="#struct.unpack" title="struct.unpack"><code class="xref py py-func docutils literal notranslate"><span class="pre">unpack()</span></code></a>,<code class="docutils literal notranslate"><span class="pre">'p'</span></code> 格式字符会消耗 <code class="docutils literal notranslate"><span class="pre">count</span></code> 个字节,但返回的字符串永远不会包含超过 255 个字节。</p>
|
||
<p id="index-3">对于 <code class="docutils literal notranslate"><span class="pre">'?'</span></code> 格式字符,返回值为 <a class="reference internal" href="constants.html#True" title="True"><code class="xref py py-const docutils literal notranslate"><span class="pre">True</span></code></a> 或 <a class="reference internal" href="constants.html#False" title="False"><code class="xref py py-const docutils literal notranslate"><span class="pre">False</span></code></a>。 在打包时将会使用参数对象的逻辑值。 以本机或标准 bool 类型表示的 0 或 1 将被打包,任何非零值在解包时将为 <code class="docutils literal notranslate"><span class="pre">True</span></code>。</p>
|
||
</section>
|
||
<section id="examples">
|
||
<span id="struct-examples"></span><h3>例子<a class="headerlink" href="#examples" title="永久链接至标题">¶</a></h3>
|
||
<div class="admonition note">
|
||
<p class="admonition-title">注解</p>
|
||
<p>所有示例都假定使用一台大端机器的本机字节顺序、大小和对齐方式。</p>
|
||
</div>
|
||
<p>打包/解包三个整数的基础示例:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">struct</span> <span class="kn">import</span> <span class="o">*</span>
|
||
<span class="gp">>>> </span><span class="n">pack</span><span class="p">(</span><span class="s1">'hhl'</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">)</span>
|
||
<span class="go">b'\x00\x01\x00\x02\x00\x00\x00\x03'</span>
|
||
<span class="gp">>>> </span><span class="n">unpack</span><span class="p">(</span><span class="s1">'hhl'</span><span class="p">,</span> <span class="sa">b</span><span class="s1">'</span><span class="se">\x00\x01\x00\x02\x00\x00\x00\x03</span><span class="s1">'</span><span class="p">)</span>
|
||
<span class="go">(1, 2, 3)</span>
|
||
<span class="gp">>>> </span><span class="n">calcsize</span><span class="p">(</span><span class="s1">'hhl'</span><span class="p">)</span>
|
||
<span class="go">8</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>解包的字段可通过将它们赋值给变量或将结果包装为一个具名元组来命名:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">record</span> <span class="o">=</span> <span class="sa">b</span><span class="s1">'raymond </span><span class="se">\x32\x12\x08\x01\x08</span><span class="s1">'</span>
|
||
<span class="gp">>>> </span><span class="n">name</span><span class="p">,</span> <span class="n">serialnum</span><span class="p">,</span> <span class="n">school</span><span class="p">,</span> <span class="n">gradelevel</span> <span class="o">=</span> <span class="n">unpack</span><span class="p">(</span><span class="s1">'<10sHHb'</span><span class="p">,</span> <span class="n">record</span><span class="p">)</span>
|
||
|
||
<span class="gp">>>> </span><span class="kn">from</span> <span class="nn">collections</span> <span class="kn">import</span> <span class="n">namedtuple</span>
|
||
<span class="gp">>>> </span><span class="n">Student</span> <span class="o">=</span> <span class="n">namedtuple</span><span class="p">(</span><span class="s1">'Student'</span><span class="p">,</span> <span class="s1">'name serialnum school gradelevel'</span><span class="p">)</span>
|
||
<span class="gp">>>> </span><span class="n">Student</span><span class="o">.</span><span class="n">_make</span><span class="p">(</span><span class="n">unpack</span><span class="p">(</span><span class="s1">'<10sHHb'</span><span class="p">,</span> <span class="n">record</span><span class="p">))</span>
|
||
<span class="go">Student(name=b'raymond ', serialnum=4658, school=264, gradelevel=8)</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>格式字符的顺序可能对大小产生影响,因为满足对齐要求所需的填充是不同的:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">pack</span><span class="p">(</span><span class="s1">'ci'</span><span class="p">,</span> <span class="sa">b</span><span class="s1">'*'</span><span class="p">,</span> <span class="mh">0x12131415</span><span class="p">)</span>
|
||
<span class="go">b'*\x00\x00\x00\x12\x13\x14\x15'</span>
|
||
<span class="gp">>>> </span><span class="n">pack</span><span class="p">(</span><span class="s1">'ic'</span><span class="p">,</span> <span class="mh">0x12131415</span><span class="p">,</span> <span class="sa">b</span><span class="s1">'*'</span><span class="p">)</span>
|
||
<span class="go">b'\x12\x13\x14\x15*'</span>
|
||
<span class="gp">>>> </span><span class="n">calcsize</span><span class="p">(</span><span class="s1">'ci'</span><span class="p">)</span>
|
||
<span class="go">8</span>
|
||
<span class="gp">>>> </span><span class="n">calcsize</span><span class="p">(</span><span class="s1">'ic'</span><span class="p">)</span>
|
||
<span class="go">5</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>以下格式 <code class="docutils literal notranslate"><span class="pre">'llh0l'</span></code> 指定在末尾有两个填充字节,假定 long 类型按 4 个字节的边界对齐:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">pack</span><span class="p">(</span><span class="s1">'llh0l'</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">)</span>
|
||
<span class="go">b'\x00\x00\x00\x01\x00\x00\x00\x02\x00\x03\x00\x00'</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>这仅当本机大小和对齐方式生效时才会起作用;标准大小和对齐方式并不会强制进行任何对齐。</p>
|
||
<div class="admonition seealso">
|
||
<p class="admonition-title">参见</p>
|
||
<dl class="simple">
|
||
<dt>模块 <a class="reference internal" href="array.html#module-array" title="array: Space efficient arrays of uniformly typed numeric values."><code class="xref py py-mod docutils literal notranslate"><span class="pre">array</span></code></a></dt><dd><p>被打包为二进制存储的同质数据。</p>
|
||
</dd>
|
||
<dt><a class="reference internal" href="xdrlib.html#module-xdrlib" title="xdrlib: Encoders and decoders for the External Data Representation (XDR)."><code class="xref py py-mod docutils literal notranslate"><span class="pre">xdrlib</span></code></a> 模块</dt><dd><p>打包和解包 XDR 数据。</p>
|
||
</dd>
|
||
</dl>
|
||
</div>
|
||
</section>
|
||
</section>
|
||
<section id="classes">
|
||
<span id="struct-objects"></span><h2>类<a class="headerlink" href="#classes" title="永久链接至标题">¶</a></h2>
|
||
<p><a class="reference internal" href="#module-struct" title="struct: Interpret bytes as packed binary data."><code class="xref py py-mod docutils literal notranslate"><span class="pre">struct</span></code></a> 模块还定义了以下类型:</p>
|
||
<dl class="class">
|
||
<dt id="struct.Struct">
|
||
<em class="property">class </em><code class="sig-prename descclassname">struct.</code><code class="sig-name descname">Struct</code><span class="sig-paren">(</span><em class="sig-param">format</em><span class="sig-paren">)</span><a class="headerlink" href="#struct.Struct" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>返回一个新的 Struct 对象,它会根据格式字符串 <em>format</em> 来写入和读取二进制数据。 一次性地创建 Struct 对象并调用其方法相比使用同样的格式调用 <a class="reference internal" href="#module-struct" title="struct: Interpret bytes as packed binary data."><code class="xref py py-mod docutils literal notranslate"><span class="pre">struct</span></code></a> 函数更为高效,因为这样格式字符串只需被编译一次。</p>
|
||
<div class="admonition note">
|
||
<p class="admonition-title">注解</p>
|
||
<p>传递给 <a class="reference internal" href="#struct.Struct" title="struct.Struct"><code class="xref py py-class docutils literal notranslate"><span class="pre">Struct</span></code></a> 和模块层级函数的已编译版最新格式字符串会被缓存,因此只使用少量格式字符串的程序无需担心重用单独的 <a class="reference internal" href="#struct.Struct" title="struct.Struct"><code class="xref py py-class docutils literal notranslate"><span class="pre">Struct</span></code></a> 实例。</p>
|
||
</div>
|
||
<p>已编译的 Struct 对象支持以下方法和属性:</p>
|
||
<dl class="method">
|
||
<dt id="struct.Struct.pack">
|
||
<code class="sig-name descname">pack</code><span class="sig-paren">(</span><em class="sig-param">v1</em>, <em class="sig-param">v2</em>, <em class="sig-param">...</em><span class="sig-paren">)</span><a class="headerlink" href="#struct.Struct.pack" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>等价于 <a class="reference internal" href="#struct.pack" title="struct.pack"><code class="xref py py-func docutils literal notranslate"><span class="pre">pack()</span></code></a> 函数,使用了已编译的格式。 (<code class="docutils literal notranslate"><span class="pre">len(result)</span></code> 将等于 <a class="reference internal" href="#struct.Struct.size" title="struct.Struct.size"><code class="xref py py-attr docutils literal notranslate"><span class="pre">size</span></code></a>。)</p>
|
||
</dd></dl>
|
||
|
||
<dl class="method">
|
||
<dt id="struct.Struct.pack_into">
|
||
<code class="sig-name descname">pack_into</code><span class="sig-paren">(</span><em class="sig-param">buffer</em>, <em class="sig-param">offset</em>, <em class="sig-param">v1</em>, <em class="sig-param">v2</em>, <em class="sig-param">...</em><span class="sig-paren">)</span><a class="headerlink" href="#struct.Struct.pack_into" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>等价于 <a class="reference internal" href="#struct.pack_into" title="struct.pack_into"><code class="xref py py-func docutils literal notranslate"><span class="pre">pack_into()</span></code></a> 函数,使用了已编译的格式。</p>
|
||
</dd></dl>
|
||
|
||
<dl class="method">
|
||
<dt id="struct.Struct.unpack">
|
||
<code class="sig-name descname">unpack</code><span class="sig-paren">(</span><em class="sig-param">buffer</em><span class="sig-paren">)</span><a class="headerlink" href="#struct.Struct.unpack" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>等价于 <a class="reference internal" href="#struct.unpack" title="struct.unpack"><code class="xref py py-func docutils literal notranslate"><span class="pre">unpack()</span></code></a> 函数,使用了已编译的格式。 缓冲区的字节大小必须等于 <a class="reference internal" href="#struct.Struct.size" title="struct.Struct.size"><code class="xref py py-attr docutils literal notranslate"><span class="pre">size</span></code></a>。</p>
|
||
</dd></dl>
|
||
|
||
<dl class="method">
|
||
<dt id="struct.Struct.unpack_from">
|
||
<code class="sig-name descname">unpack_from</code><span class="sig-paren">(</span><em class="sig-param">buffer</em>, <em class="sig-param">offset=0</em><span class="sig-paren">)</span><a class="headerlink" href="#struct.Struct.unpack_from" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>等价于 <a class="reference internal" href="#struct.unpack_from" title="struct.unpack_from"><code class="xref py py-func docutils literal notranslate"><span class="pre">unpack_from()</span></code></a> 函数,使用了已编译的格式。 缓冲区的字节大小从位置 <em>offset</em> 开始必须至少为 <a class="reference internal" href="#struct.Struct.size" title="struct.Struct.size"><code class="xref py py-attr docutils literal notranslate"><span class="pre">size</span></code></a>。</p>
|
||
</dd></dl>
|
||
|
||
<dl class="method">
|
||
<dt id="struct.Struct.iter_unpack">
|
||
<code class="sig-name descname">iter_unpack</code><span class="sig-paren">(</span><em class="sig-param">buffer</em><span class="sig-paren">)</span><a class="headerlink" href="#struct.Struct.iter_unpack" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>等价于 <a class="reference internal" href="#struct.iter_unpack" title="struct.iter_unpack"><code class="xref py py-func docutils literal notranslate"><span class="pre">iter_unpack()</span></code></a> 函数,使用了已编译的格式。 缓冲区的大小必须为 <a class="reference internal" href="#struct.Struct.size" title="struct.Struct.size"><code class="xref py py-attr docutils literal notranslate"><span class="pre">size</span></code></a> 的整数倍。</p>
|
||
<div class="versionadded">
|
||
<p><span class="versionmodified added">3.4 新版功能.</span></p>
|
||
</div>
|
||
</dd></dl>
|
||
|
||
<dl class="attribute">
|
||
<dt id="struct.Struct.format">
|
||
<code class="sig-name descname">format</code><a class="headerlink" href="#struct.Struct.format" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>用于构造此 Struct 对象的格式字符串。</p>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">在 3.7 版更改: </span>格式字符串类型现在是 <a class="reference internal" href="stdtypes.html#str" title="str"><code class="xref py py-class docutils literal notranslate"><span class="pre">str</span></code></a> 而不再是 <a class="reference internal" href="stdtypes.html#bytes" title="bytes"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code></a>。</p>
|
||
</div>
|
||
</dd></dl>
|
||
|
||
<dl class="attribute">
|
||
<dt id="struct.Struct.size">
|
||
<code class="sig-name descname">size</code><a class="headerlink" href="#struct.Struct.size" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>计算出对应于 <a class="reference internal" href="functions.html#format" title="format"><code class="xref py py-attr docutils literal notranslate"><span class="pre">format</span></code></a> 的结构大小(亦即 <a class="reference internal" href="#struct.pack" title="struct.pack"><code class="xref py py-meth docutils literal notranslate"><span class="pre">pack()</span></code></a> 方法所产生的字节串对象的大小)。</p>
|
||
</dd></dl>
|
||
|
||
</dd></dl>
|
||
|
||
</section>
|
||
</section>
|
||
|
||
|
||
</div>
|
||
</div>
|
||
</div>
|
||
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
|
||
<div class="sphinxsidebarwrapper">
|
||
<h3><a href="../contents.html">目录</a></h3>
|
||
<ul>
|
||
<li><a class="reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">struct</span></code> --- 将字节串解读为打包的二进制数据</a><ul>
|
||
<li><a class="reference internal" href="#functions-and-exceptions">函数和异常</a></li>
|
||
<li><a class="reference internal" href="#format-strings">格式字符串</a><ul>
|
||
<li><a class="reference internal" href="#byte-order-size-and-alignment">字节顺序,大小和对齐方式</a></li>
|
||
<li><a class="reference internal" href="#format-characters">格式字符</a></li>
|
||
<li><a class="reference internal" href="#examples">例子</a></li>
|
||
</ul>
|
||
</li>
|
||
<li><a class="reference internal" href="#classes">类</a></li>
|
||
</ul>
|
||
</li>
|
||
</ul>
|
||
|
||
<h4>上一个主题</h4>
|
||
<p class="topless"><a href="binary.html"
|
||
title="上一章">二进制数据服务</a></p>
|
||
<h4>下一个主题</h4>
|
||
<p class="topless"><a href="codecs.html"
|
||
title="下一章"><code class="xref py py-mod docutils literal notranslate"><span class="pre">codecs</span></code> --- 编解码器注册和相关基类</a></p>
|
||
<div role="note" aria-label="source link">
|
||
<h3>本页</h3>
|
||
<ul class="this-page-menu">
|
||
<li><a href="../bugs.html">报告 Bug</a></li>
|
||
<li>
|
||
<a href="https://github.com/python/cpython/blob/3.8/Doc/library/struct.rst"
|
||
rel="nofollow">显示源代码
|
||
</a>
|
||
</li>
|
||
</ul>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
<div class="clearer"></div>
|
||
</div>
|
||
<div class="related" role="navigation" aria-label="related navigation">
|
||
<h3>导航</h3>
|
||
<ul>
|
||
<li class="right" style="margin-right: 10px">
|
||
<a href="../genindex.html" title="总目录"
|
||
>索引</a></li>
|
||
<li class="right" >
|
||
<a href="../py-modindex.html" title="Python 模块索引"
|
||
>模块</a> |</li>
|
||
<li class="right" >
|
||
<a href="codecs.html" title="codecs --- 编解码器注册和相关基类"
|
||
>下一页</a> |</li>
|
||
<li class="right" >
|
||
<a href="binary.html" title="二进制数据服务"
|
||
>上一页</a> |</li>
|
||
|
||
<li><img src="../_static/py.svg" alt="python logo" style="vertical-align: middle; margin-top: -1px"/></li>
|
||
<li><a href="https://www.python.org/">Python</a> »</li>
|
||
<li class="switchers">
|
||
<div class="language_switcher_placeholder"></div>
|
||
<div class="version_switcher_placeholder"></div>
|
||
</li>
|
||
<li>
|
||
|
||
</li>
|
||
<li id="cpython-language-and-version">
|
||
<a href="../index.html">3.8.20 Documentation</a> »
|
||
</li>
|
||
|
||
<li class="nav-item nav-item-1"><a href="index.html" >Python 标准库</a> »</li>
|
||
<li class="nav-item nav-item-2"><a href="binary.html" >二进制数据服务</a> »</li>
|
||
<li class="right">
|
||
|
||
|
||
<div class="inline-search" role="search">
|
||
<form class="inline-search" action="../search.html" method="get">
|
||
<input placeholder="快速搜索" aria-label="快速搜索" type="text" name="q" />
|
||
<input type="submit" value="转向" />
|
||
<input type="hidden" name="check_keywords" value="yes" />
|
||
<input type="hidden" name="area" value="default" />
|
||
</form>
|
||
</div>
|
||
|
|
||
</li>
|
||
|
||
</ul>
|
||
</div>
|
||
<div class="footer">
|
||
© <a href="../copyright.html">版权所有</a> 2001-2024, Python Software Foundation.
|
||
<br />
|
||
This page is licensed under the Python Software Foundation License Version 2.
|
||
<br />
|
||
Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.
|
||
<br />
|
||
|
||
<br />
|
||
|
||
The Python Software Foundation is a non-profit corporation.
|
||
<a href="https://www.python.org/psf/donations/">Please donate.</a>
|
||
<br />
|
||
<br />
|
||
|
||
最后更新于 12月 09, 2024.
|
||
<a href="https://docs.python.org/3/bugs.html">Found a bug</a>?
|
||
<br />
|
||
|
||
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 2.4.4.
|
||
</div>
|
||
|
||
</body>
|
||
</html> |