gooderp18绿色标准版
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

1010 lignes
74KB

  1. <!DOCTYPE html>
  2. <html lang="en" data-content_root="../">
  3. <head>
  4. <meta charset="utf-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="viewport" content="width=device-width, initial-scale=1" />
  6. <meta property="og:title" content="struct — Interpret bytes as packed binary data" />
  7. <meta property="og:type" content="website" />
  8. <meta property="og:url" content="https://docs.python.org/3/library/struct.html" />
  9. <meta property="og:site_name" content="Python documentation" />
  10. <meta property="og:description" content="Source code: Lib/struct.py This module converts between Python values and C structs represented as Python bytes objects. Compact format strings describe the intended conversions to/from Python valu..." />
  11. <meta property="og:image" content="https://docs.python.org/3/_static/og-image.png" />
  12. <meta property="og:image:alt" content="Python documentation" />
  13. <meta name="description" content="Source code: Lib/struct.py This module converts between Python values and C structs represented as Python bytes objects. Compact format strings describe the intended conversions to/from Python valu..." />
  14. <meta property="og:image:width" content="200" />
  15. <meta property="og:image:height" content="200" />
  16. <meta name="theme-color" content="#3776ab" />
  17. <title>struct — Interpret bytes as packed binary data &#8212; Python 3.12.3 documentation</title><meta name="viewport" content="width=device-width, initial-scale=1.0">
  18. <link rel="stylesheet" type="text/css" href="../_static/pygments.css?v=80d5e7a1" />
  19. <link rel="stylesheet" type="text/css" href="../_static/pydoctheme.css?v=bb723527" />
  20. <link id="pygments_dark_css" media="(prefers-color-scheme: dark)" rel="stylesheet" type="text/css" href="../_static/pygments_dark.css?v=b20cc3f5" />
  21. <script src="../_static/documentation_options.js?v=2c828074"></script>
  22. <script src="../_static/doctools.js?v=888ff710"></script>
  23. <script src="../_static/sphinx_highlight.js?v=dc90522c"></script>
  24. <script src="../_static/sidebar.js"></script>
  25. <link rel="search" type="application/opensearchdescription+xml"
  26. title="Search within Python 3.12.3 documentation"
  27. href="../_static/opensearch.xml"/>
  28. <link rel="author" title="About these documents" href="../about.html" />
  29. <link rel="index" title="Index" href="../genindex.html" />
  30. <link rel="search" title="Search" href="../search.html" />
  31. <link rel="copyright" title="Copyright" href="../copyright.html" />
  32. <link rel="next" title="codecs — Codec registry and base classes" href="codecs.html" />
  33. <link rel="prev" title="Binary Data Services" href="binary.html" />
  34. <link rel="canonical" href="https://docs.python.org/3/library/struct.html" />
  35. <style>
  36. @media only screen {
  37. table.full-width-table {
  38. width: 100%;
  39. }
  40. }
  41. </style>
  42. <link rel="stylesheet" href="../_static/pydoctheme_dark.css" media="(prefers-color-scheme: dark)" id="pydoctheme_dark_css">
  43. <link rel="shortcut icon" type="image/png" href="../_static/py.svg" />
  44. <script type="text/javascript" src="../_static/copybutton.js"></script>
  45. <script type="text/javascript" src="../_static/menu.js"></script>
  46. <script type="text/javascript" src="../_static/search-focus.js"></script>
  47. <script type="text/javascript" src="../_static/themetoggle.js"></script>
  48. </head>
  49. <body>
  50. <div class="mobile-nav">
  51. <input type="checkbox" id="menuToggler" class="toggler__input" aria-controls="navigation"
  52. aria-pressed="false" aria-expanded="false" role="button" aria-label="Menu" />
  53. <nav class="nav-content" role="navigation">
  54. <label for="menuToggler" class="toggler__label">
  55. <span></span>
  56. </label>
  57. <span class="nav-items-wrapper">
  58. <a href="https://www.python.org/" class="nav-logo">
  59. <img src="../_static/py.svg" alt="Python logo"/>
  60. </a>
  61. <span class="version_switcher_placeholder"></span>
  62. <form role="search" class="search" action="../search.html" method="get">
  63. <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" class="search-icon">
  64. <path fill-rule="nonzero" fill="currentColor" 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"></path>
  65. </svg>
  66. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" />
  67. <input type="submit" value="Go"/>
  68. </form>
  69. </span>
  70. </nav>
  71. <div class="menu-wrapper">
  72. <nav class="menu" role="navigation" aria-label="main navigation">
  73. <div class="language_switcher_placeholder"></div>
  74. <label class="theme-selector-label">
  75. Theme
  76. <select class="theme-selector" oninput="activateTheme(this.value)">
  77. <option value="auto" selected>Auto</option>
  78. <option value="light">Light</option>
  79. <option value="dark">Dark</option>
  80. </select>
  81. </label>
  82. <div>
  83. <h3><a href="../contents.html">Table of Contents</a></h3>
  84. <ul>
  85. <li><a class="reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">struct</span></code> — Interpret bytes as packed binary data</a><ul>
  86. <li><a class="reference internal" href="#functions-and-exceptions">Functions and Exceptions</a></li>
  87. <li><a class="reference internal" href="#format-strings">Format Strings</a><ul>
  88. <li><a class="reference internal" href="#byte-order-size-and-alignment">Byte Order, Size, and Alignment</a></li>
  89. <li><a class="reference internal" href="#format-characters">Format Characters</a></li>
  90. <li><a class="reference internal" href="#examples">Examples</a></li>
  91. </ul>
  92. </li>
  93. <li><a class="reference internal" href="#applications">Applications</a><ul>
  94. <li><a class="reference internal" href="#native-formats">Native Formats</a></li>
  95. <li><a class="reference internal" href="#standard-formats">Standard Formats</a></li>
  96. </ul>
  97. </li>
  98. <li><a class="reference internal" href="#classes">Classes</a></li>
  99. </ul>
  100. </li>
  101. </ul>
  102. </div>
  103. <div>
  104. <h4>Previous topic</h4>
  105. <p class="topless"><a href="binary.html"
  106. title="previous chapter">Binary Data Services</a></p>
  107. </div>
  108. <div>
  109. <h4>Next topic</h4>
  110. <p class="topless"><a href="codecs.html"
  111. title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">codecs</span></code> — Codec registry and base classes</a></p>
  112. </div>
  113. <div role="note" aria-label="source link">
  114. <h3>This Page</h3>
  115. <ul class="this-page-menu">
  116. <li><a href="../bugs.html">Report a Bug</a></li>
  117. <li>
  118. <a href="https://github.com/python/cpython/blob/main/Doc/library/struct.rst"
  119. rel="nofollow">Show Source
  120. </a>
  121. </li>
  122. </ul>
  123. </div>
  124. </nav>
  125. </div>
  126. </div>
  127. <div class="related" role="navigation" aria-label="related navigation">
  128. <h3>Navigation</h3>
  129. <ul>
  130. <li class="right" style="margin-right: 10px">
  131. <a href="../genindex.html" title="General Index"
  132. accesskey="I">index</a></li>
  133. <li class="right" >
  134. <a href="../py-modindex.html" title="Python Module Index"
  135. >modules</a> |</li>
  136. <li class="right" >
  137. <a href="codecs.html" title="codecs — Codec registry and base classes"
  138. accesskey="N">next</a> |</li>
  139. <li class="right" >
  140. <a href="binary.html" title="Binary Data Services"
  141. accesskey="P">previous</a> |</li>
  142. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  143. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  144. <li class="switchers">
  145. <div class="language_switcher_placeholder"></div>
  146. <div class="version_switcher_placeholder"></div>
  147. </li>
  148. <li>
  149. </li>
  150. <li id="cpython-language-and-version">
  151. <a href="../index.html">3.12.3 Documentation</a> &#187;
  152. </li>
  153. <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
  154. <li class="nav-item nav-item-2"><a href="binary.html" accesskey="U">Binary Data Services</a> &#187;</li>
  155. <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">struct</span></code> — Interpret bytes as packed binary data</a></li>
  156. <li class="right">
  157. <div class="inline-search" role="search">
  158. <form class="inline-search" action="../search.html" method="get">
  159. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  160. <input type="submit" value="Go" />
  161. </form>
  162. </div>
  163. |
  164. </li>
  165. <li class="right">
  166. <label class="theme-selector-label">
  167. Theme
  168. <select class="theme-selector" oninput="activateTheme(this.value)">
  169. <option value="auto" selected>Auto</option>
  170. <option value="light">Light</option>
  171. <option value="dark">Dark</option>
  172. </select>
  173. </label> |</li>
  174. </ul>
  175. </div>
  176. <div class="document">
  177. <div class="documentwrapper">
  178. <div class="bodywrapper">
  179. <div class="body" role="main">
  180. <section id="module-struct">
  181. <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> — Interpret bytes as packed binary data<a class="headerlink" href="#module-struct" title="Link to this heading">¶</a></h1>
  182. <p><strong>Source code:</strong> <a class="reference external" href="https://github.com/python/cpython/tree/3.12/Lib/struct.py">Lib/struct.py</a></p>
  183. <hr class="docutils" id="index-0" />
  184. <p>This module converts between Python values and C structs represented
  185. as 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> objects. Compact <a class="reference internal" href="#struct-format-strings"><span class="std std-ref">format strings</span></a>
  186. describe the intended conversions to/from Python values.
  187. The module’s functions and objects can be used for two largely
  188. distinct applications, data exchange with external sources (files or
  189. network connections), or data transfer between the Python application
  190. and the C layer.</p>
  191. <div class="admonition note">
  192. <p class="admonition-title">Note</p>
  193. <p>When no prefix character is given, native mode is the default. It
  194. packs or unpacks data based on the platform and compiler on which
  195. the Python interpreter was built.
  196. The result of packing a given C struct includes pad bytes which
  197. maintain proper alignment for the C types involved; similarly,
  198. alignment is taken into account when unpacking. In contrast, when
  199. communicating data between external sources, the programmer is
  200. responsible for defining byte ordering and padding between elements.
  201. See <a class="reference internal" href="#struct-alignment"><span class="std std-ref">Byte Order, Size, and Alignment</span></a> for details.</p>
  202. </div>
  203. <p>Several <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> functions (and methods of <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>) take a <em>buffer</em>
  204. argument. This refers to objects that implement the <a class="reference internal" href="../c-api/buffer.html#bufferobjects"><span class="std std-ref">Buffer Protocol</span></a> and
  205. provide either a readable or read-writable buffer. The most common types used
  206. for that purpose are <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> and <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>, but many other types
  207. that can be viewed as an array of bytes implement the buffer protocol, so that
  208. they can be read/filled without additional copying from 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> object.</p>
  209. <section id="functions-and-exceptions">
  210. <h2>Functions and Exceptions<a class="headerlink" href="#functions-and-exceptions" title="Link to this heading">¶</a></h2>
  211. <p>The module defines the following exception and functions:</p>
  212. <dl class="py exception">
  213. <dt class="sig sig-object py" id="struct.error">
  214. <em class="property"><span class="pre">exception</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">struct.</span></span><span class="sig-name descname"><span class="pre">error</span></span><a class="headerlink" href="#struct.error" title="Link to this definition">¶</a></dt>
  215. <dd><p>Exception raised on various occasions; argument is a string describing what
  216. is wrong.</p>
  217. </dd></dl>
  218. <dl class="py function">
  219. <dt class="sig sig-object py" id="struct.pack">
  220. <span class="sig-prename descclassname"><span class="pre">struct.</span></span><span class="sig-name descname"><span class="pre">pack</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">format</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">v1</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">v2</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">...</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#struct.pack" title="Link to this definition">¶</a></dt>
  221. <dd><p>Return a bytes object containing the values <em>v1</em>, <em>v2</em>, … packed according
  222. to the format string <em>format</em>. The arguments must match the values required by
  223. the format exactly.</p>
  224. </dd></dl>
  225. <dl class="py function">
  226. <dt class="sig sig-object py" id="struct.pack_into">
  227. <span class="sig-prename descclassname"><span class="pre">struct.</span></span><span class="sig-name descname"><span class="pre">pack_into</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">format</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">buffer</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">offset</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">v1</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">v2</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">...</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#struct.pack_into" title="Link to this definition">¶</a></dt>
  228. <dd><p>Pack the values <em>v1</em>, <em>v2</em>, … according to the format string <em>format</em> and
  229. write the packed bytes into the writable buffer <em>buffer</em> starting at
  230. position <em>offset</em>. Note that <em>offset</em> is a required argument.</p>
  231. </dd></dl>
  232. <dl class="py function">
  233. <dt class="sig sig-object py" id="struct.unpack">
  234. <span class="sig-prename descclassname"><span class="pre">struct.</span></span><span class="sig-name descname"><span class="pre">unpack</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">format</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">buffer</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#struct.unpack" title="Link to this definition">¶</a></dt>
  235. <dd><p>Unpack from the buffer <em>buffer</em> (presumably packed by <code class="docutils literal notranslate"><span class="pre">pack(format,</span> <span class="pre">...)</span></code>)
  236. according to the format string <em>format</em>. The result is a tuple even if it
  237. contains exactly one item. The buffer’s size in bytes must match the
  238. size required by the format, as reflected by <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>
  239. </dd></dl>
  240. <dl class="py function">
  241. <dt class="sig sig-object py" id="struct.unpack_from">
  242. <span class="sig-prename descclassname"><span class="pre">struct.</span></span><span class="sig-name descname"><span class="pre">unpack_from</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">format</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">/</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">buffer</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">offset</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">0</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#struct.unpack_from" title="Link to this definition">¶</a></dt>
  243. <dd><p>Unpack from <em>buffer</em> starting at position <em>offset</em>, according to the format
  244. string <em>format</em>. The result is a tuple even if it contains exactly one
  245. item. The buffer’s size in bytes, starting at position <em>offset</em>, must be at
  246. least the size required by the format, as reflected by <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>
  247. </dd></dl>
  248. <dl class="py function">
  249. <dt class="sig sig-object py" id="struct.iter_unpack">
  250. <span class="sig-prename descclassname"><span class="pre">struct.</span></span><span class="sig-name descname"><span class="pre">iter_unpack</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">format</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">buffer</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#struct.iter_unpack" title="Link to this definition">¶</a></dt>
  251. <dd><p>Iteratively unpack from the buffer <em>buffer</em> according to the format
  252. string <em>format</em>. This function returns an iterator which will read
  253. equally sized chunks from the buffer until all its contents have been
  254. consumed. The buffer’s size in bytes must be a multiple of the size
  255. required by the format, as reflected by <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>
  256. <p>Each iteration yields a tuple as specified by the format string.</p>
  257. <div class="versionadded">
  258. <p><span class="versionmodified added">New in version 3.4.</span></p>
  259. </div>
  260. </dd></dl>
  261. <dl class="py function">
  262. <dt class="sig sig-object py" id="struct.calcsize">
  263. <span class="sig-prename descclassname"><span class="pre">struct.</span></span><span class="sig-name descname"><span class="pre">calcsize</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">format</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#struct.calcsize" title="Link to this definition">¶</a></dt>
  264. <dd><p>Return the size of the struct (and hence of the bytes object produced by
  265. <code class="docutils literal notranslate"><span class="pre">pack(format,</span> <span class="pre">...)</span></code>) corresponding to the format string <em>format</em>.</p>
  266. </dd></dl>
  267. </section>
  268. <section id="format-strings">
  269. <span id="struct-format-strings"></span><h2>Format Strings<a class="headerlink" href="#format-strings" title="Link to this heading">¶</a></h2>
  270. <p>Format strings describe the data layout when
  271. packing and unpacking data. They are built up from <a class="reference internal" href="#format-characters"><span class="std std-ref">format characters</span></a>,
  272. which specify the type of data being packed/unpacked. In addition,
  273. special characters control the <a class="reference internal" href="#struct-alignment"><span class="std std-ref">byte order, size and alignment</span></a>.
  274. Each format string consists of an optional prefix character which
  275. describes the overall properties of the data and one or more format
  276. characters which describe the actual data values and padding.</p>
  277. <section id="byte-order-size-and-alignment">
  278. <span id="struct-alignment"></span><h3>Byte Order, Size, and Alignment<a class="headerlink" href="#byte-order-size-and-alignment" title="Link to this heading">¶</a></h3>
  279. <p>By default, C types are represented in the machine’s native format and byte
  280. order, and properly aligned by skipping pad bytes if necessary (according to the
  281. rules used by the C compiler).
  282. This behavior is chosen so
  283. that the bytes of a packed struct correspond exactly to the memory layout
  284. of the corresponding C struct.
  285. Whether to use native byte ordering
  286. and padding or standard formats depends on the application.</p>
  287. <p id="index-1">Alternatively, the first character of the format string can be used to indicate
  288. the byte order, size and alignment of the packed data, according to the
  289. following table:</p>
  290. <table class="docutils align-default">
  291. <thead>
  292. <tr class="row-odd"><th class="head"><p>Character</p></th>
  293. <th class="head"><p>Byte order</p></th>
  294. <th class="head"><p>Size</p></th>
  295. <th class="head"><p>Alignment</p></th>
  296. </tr>
  297. </thead>
  298. <tbody>
  299. <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">&#64;</span></code></p></td>
  300. <td><p>native</p></td>
  301. <td><p>native</p></td>
  302. <td><p>native</p></td>
  303. </tr>
  304. <tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">=</span></code></p></td>
  305. <td><p>native</p></td>
  306. <td><p>standard</p></td>
  307. <td><p>none</p></td>
  308. </tr>
  309. <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">&lt;</span></code></p></td>
  310. <td><p>little-endian</p></td>
  311. <td><p>standard</p></td>
  312. <td><p>none</p></td>
  313. </tr>
  314. <tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">&gt;</span></code></p></td>
  315. <td><p>big-endian</p></td>
  316. <td><p>standard</p></td>
  317. <td><p>none</p></td>
  318. </tr>
  319. <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">!</span></code></p></td>
  320. <td><p>network (= big-endian)</p></td>
  321. <td><p>standard</p></td>
  322. <td><p>none</p></td>
  323. </tr>
  324. </tbody>
  325. </table>
  326. <p>If the first character is not one of these, <code class="docutils literal notranslate"><span class="pre">'&#64;'</span></code> is assumed.</p>
  327. <div class="admonition note">
  328. <p class="admonition-title">Note</p>
  329. <p>The number 1023 (<code class="docutils literal notranslate"><span class="pre">0x3ff</span></code> in hexadecimal) has the following byte representations:</p>
  330. <ul class="simple">
  331. <li><p><code class="docutils literal notranslate"><span class="pre">03</span> <span class="pre">ff</span></code> in big-endian (<code class="docutils literal notranslate"><span class="pre">&gt;</span></code>)</p></li>
  332. <li><p><code class="docutils literal notranslate"><span class="pre">ff</span> <span class="pre">03</span></code> in little-endian (<code class="docutils literal notranslate"><span class="pre">&lt;</span></code>)</p></li>
  333. </ul>
  334. <p>Python example:</p>
  335. <div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">struct</span>
  336. <span class="gp">&gt;&gt;&gt; </span><span class="n">struct</span><span class="o">.</span><span class="n">pack</span><span class="p">(</span><span class="s1">&#39;&gt;h&#39;</span><span class="p">,</span> <span class="mi">1023</span><span class="p">)</span>
  337. <span class="go">b&#39;\x03\xff&#39;</span>
  338. <span class="gp">&gt;&gt;&gt; </span><span class="n">struct</span><span class="o">.</span><span class="n">pack</span><span class="p">(</span><span class="s1">&#39;&lt;h&#39;</span><span class="p">,</span> <span class="mi">1023</span><span class="p">)</span>
  339. <span class="go">b&#39;\xff\x03&#39;</span>
  340. </pre></div>
  341. </div>
  342. </div>
  343. <p>Native byte order is big-endian or little-endian, depending on the
  344. host system. For example, Intel x86, AMD64 (x86-64), and Apple M1 are
  345. little-endian; IBM z and many legacy architectures are big-endian.
  346. Use <a class="reference internal" href="sys.html#sys.byteorder" title="sys.byteorder"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.byteorder</span></code></a> to check the endianness of your system.</p>
  347. <p>Native size and alignment are determined using the C compiler’s
  348. <code class="docutils literal notranslate"><span class="pre">sizeof</span></code> expression. This is always combined with native byte order.</p>
  349. <p>Standard size depends only on the format character; see the table in
  350. the <a class="reference internal" href="#format-characters"><span class="std std-ref">Format Characters</span></a> section.</p>
  351. <p>Note the difference between <code class="docutils literal notranslate"><span class="pre">'&#64;'</span></code> and <code class="docutils literal notranslate"><span class="pre">'='</span></code>: both use native byte order, but
  352. the size and alignment of the latter is standardized.</p>
  353. <p>The form <code class="docutils literal notranslate"><span class="pre">'!'</span></code> represents the network byte order which is always big-endian
  354. as defined in <a class="reference external" href="https://datatracker.ietf.org/doc/html/rfc1700">IETF RFC 1700</a>.</p>
  355. <p>There is no way to indicate non-native byte order (force byte-swapping); use the
  356. appropriate choice of <code class="docutils literal notranslate"><span class="pre">'&lt;'</span></code> or <code class="docutils literal notranslate"><span class="pre">'&gt;'</span></code>.</p>
  357. <p>Notes:</p>
  358. <ol class="arabic simple">
  359. <li><p>Padding is only automatically added between successive structure members.
  360. No padding is added at the beginning or the end of the encoded struct.</p></li>
  361. <li><p>No padding is added when using non-native size and alignment, e.g.
  362. with ‘&lt;’, ‘&gt;’, ‘=’, and ‘!’.</p></li>
  363. <li><p>To align the end of a structure to the alignment requirement of a
  364. particular type, end the format with the code for that type with a repeat
  365. count of zero. See <a class="reference internal" href="#struct-examples"><span class="std std-ref">Examples</span></a>.</p></li>
  366. </ol>
  367. </section>
  368. <section id="format-characters">
  369. <span id="id1"></span><h3>Format Characters<a class="headerlink" href="#format-characters" title="Link to this heading">¶</a></h3>
  370. <p>Format characters have the following meaning; the conversion between C and
  371. Python values should be obvious given their types. The ‘Standard size’ column
  372. refers to the size of the packed value in bytes when using standard size; that
  373. is, when the format string starts with one of <code class="docutils literal notranslate"><span class="pre">'&lt;'</span></code>, <code class="docutils literal notranslate"><span class="pre">'&gt;'</span></code>, <code class="docutils literal notranslate"><span class="pre">'!'</span></code> or
  374. <code class="docutils literal notranslate"><span class="pre">'='</span></code>. When using native size, the size of the packed value is
  375. platform-dependent.</p>
  376. <table class="docutils align-default">
  377. <thead>
  378. <tr class="row-odd"><th class="head"><p>Format</p></th>
  379. <th class="head"><p>C Type</p></th>
  380. <th class="head"><p>Python type</p></th>
  381. <th class="head"><p>Standard size</p></th>
  382. <th class="head"><p>Notes</p></th>
  383. </tr>
  384. </thead>
  385. <tbody>
  386. <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">x</span></code></p></td>
  387. <td><p>pad byte</p></td>
  388. <td><p>no value</p></td>
  389. <td></td>
  390. <td><p>(7)</p></td>
  391. </tr>
  392. <tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">c</span></code></p></td>
  393. <td><p><span class="c-expr sig sig-inline c"><span class="kt">char</span></span></p></td>
  394. <td><p>bytes of length 1</p></td>
  395. <td><p>1</p></td>
  396. <td></td>
  397. </tr>
  398. <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">b</span></code></p></td>
  399. <td><p><span class="c-expr sig sig-inline c"><span class="kt">signed</span><span class="w"> </span><span class="kt">char</span></span></p></td>
  400. <td><p>integer</p></td>
  401. <td><p>1</p></td>
  402. <td><p>(1), (2)</p></td>
  403. </tr>
  404. <tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">B</span></code></p></td>
  405. <td><p><span class="c-expr sig sig-inline c"><span class="kt">unsigned</span><span class="w"> </span><span class="kt">char</span></span></p></td>
  406. <td><p>integer</p></td>
  407. <td><p>1</p></td>
  408. <td><p>(2)</p></td>
  409. </tr>
  410. <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">?</span></code></p></td>
  411. <td><p><span class="c-expr sig sig-inline c"><span class="kt">_Bool</span></span></p></td>
  412. <td><p>bool</p></td>
  413. <td><p>1</p></td>
  414. <td><p>(1)</p></td>
  415. </tr>
  416. <tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">h</span></code></p></td>
  417. <td><p><span class="c-expr sig sig-inline c"><span class="kt">short</span></span></p></td>
  418. <td><p>integer</p></td>
  419. <td><p>2</p></td>
  420. <td><p>(2)</p></td>
  421. </tr>
  422. <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">H</span></code></p></td>
  423. <td><p><span class="c-expr sig sig-inline c"><span class="kt">unsigned</span><span class="w"> </span><span class="kt">short</span></span></p></td>
  424. <td><p>integer</p></td>
  425. <td><p>2</p></td>
  426. <td><p>(2)</p></td>
  427. </tr>
  428. <tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">i</span></code></p></td>
  429. <td><p><span class="c-expr sig sig-inline c"><span class="kt">int</span></span></p></td>
  430. <td><p>integer</p></td>
  431. <td><p>4</p></td>
  432. <td><p>(2)</p></td>
  433. </tr>
  434. <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">I</span></code></p></td>
  435. <td><p><span class="c-expr sig sig-inline c"><span class="kt">unsigned</span><span class="w"> </span><span class="kt">int</span></span></p></td>
  436. <td><p>integer</p></td>
  437. <td><p>4</p></td>
  438. <td><p>(2)</p></td>
  439. </tr>
  440. <tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">l</span></code></p></td>
  441. <td><p><span class="c-expr sig sig-inline c"><span class="kt">long</span></span></p></td>
  442. <td><p>integer</p></td>
  443. <td><p>4</p></td>
  444. <td><p>(2)</p></td>
  445. </tr>
  446. <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">L</span></code></p></td>
  447. <td><p><span class="c-expr sig sig-inline c"><span class="kt">unsigned</span><span class="w"> </span><span class="kt">long</span></span></p></td>
  448. <td><p>integer</p></td>
  449. <td><p>4</p></td>
  450. <td><p>(2)</p></td>
  451. </tr>
  452. <tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">q</span></code></p></td>
  453. <td><p><span class="c-expr sig sig-inline c"><span class="kt">long</span><span class="w"> </span><span class="kt">long</span></span></p></td>
  454. <td><p>integer</p></td>
  455. <td><p>8</p></td>
  456. <td><p>(2)</p></td>
  457. </tr>
  458. <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">Q</span></code></p></td>
  459. <td><p><span class="c-expr sig sig-inline c"><span class="kt">unsigned</span><span class="w"> </span><span class="kt">long</span><span class="w"> </span><span class="kt">long</span></span></p></td>
  460. <td><p>integer</p></td>
  461. <td><p>8</p></td>
  462. <td><p>(2)</p></td>
  463. </tr>
  464. <tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">n</span></code></p></td>
  465. <td><p><code class="xref c c-type docutils literal notranslate"><span class="pre">ssize_t</span></code></p></td>
  466. <td><p>integer</p></td>
  467. <td></td>
  468. <td><p>(3)</p></td>
  469. </tr>
  470. <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">N</span></code></p></td>
  471. <td><p><code class="xref c c-type docutils literal notranslate"><span class="pre">size_t</span></code></p></td>
  472. <td><p>integer</p></td>
  473. <td></td>
  474. <td><p>(3)</p></td>
  475. </tr>
  476. <tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">e</span></code></p></td>
  477. <td><p>(6)</p></td>
  478. <td><p>float</p></td>
  479. <td><p>2</p></td>
  480. <td><p>(4)</p></td>
  481. </tr>
  482. <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">f</span></code></p></td>
  483. <td><p><span class="c-expr sig sig-inline c"><span class="kt">float</span></span></p></td>
  484. <td><p>float</p></td>
  485. <td><p>4</p></td>
  486. <td><p>(4)</p></td>
  487. </tr>
  488. <tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">d</span></code></p></td>
  489. <td><p><span class="c-expr sig sig-inline c"><span class="kt">double</span></span></p></td>
  490. <td><p>float</p></td>
  491. <td><p>8</p></td>
  492. <td><p>(4)</p></td>
  493. </tr>
  494. <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">s</span></code></p></td>
  495. <td><p><span class="c-expr sig sig-inline c"><span class="kt">char</span><span class="p">[</span><span class="p">]</span></span></p></td>
  496. <td><p>bytes</p></td>
  497. <td></td>
  498. <td><p>(9)</p></td>
  499. </tr>
  500. <tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">p</span></code></p></td>
  501. <td><p><span class="c-expr sig sig-inline c"><span class="kt">char</span><span class="p">[</span><span class="p">]</span></span></p></td>
  502. <td><p>bytes</p></td>
  503. <td></td>
  504. <td><p>(8)</p></td>
  505. </tr>
  506. <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">P</span></code></p></td>
  507. <td><p><span class="c-expr sig sig-inline c"><span class="kt">void</span><span class="p">*</span></span></p></td>
  508. <td><p>integer</p></td>
  509. <td></td>
  510. <td><p>(5)</p></td>
  511. </tr>
  512. </tbody>
  513. </table>
  514. <div class="versionchanged">
  515. <p><span class="versionmodified changed">Changed in version 3.3: </span>Added support for the <code class="docutils literal notranslate"><span class="pre">'n'</span></code> and <code class="docutils literal notranslate"><span class="pre">'N'</span></code> formats.</p>
  516. </div>
  517. <div class="versionchanged">
  518. <p><span class="versionmodified changed">Changed in version 3.6: </span>Added support for the <code class="docutils literal notranslate"><span class="pre">'e'</span></code> format.</p>
  519. </div>
  520. <p>Notes:</p>
  521. <ol class="arabic">
  522. <li><p id="index-2">The <code class="docutils literal notranslate"><span class="pre">'?'</span></code> conversion code corresponds to the <span class="c-expr sig sig-inline c"><span class="kt">_Bool</span></span> type defined by
  523. C99. If this type is not available, it is simulated using a <span class="c-expr sig sig-inline c"><span class="kt">char</span></span>. In
  524. standard mode, it is always represented by one byte.</p>
  525. </li>
  526. <li><p>When attempting to pack a non-integer using any of the integer conversion
  527. codes, if the non-integer has a <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> method then that method is
  528. called to convert the argument to an integer before packing.</p>
  529. <div class="versionchanged">
  530. <p><span class="versionmodified changed">Changed in version 3.2: </span>Added use of the <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> method for non-integers.</p>
  531. </div>
  532. </li>
  533. <li><p>The <code class="docutils literal notranslate"><span class="pre">'n'</span></code> and <code class="docutils literal notranslate"><span class="pre">'N'</span></code> conversion codes are only available for the native
  534. size (selected as the default or with the <code class="docutils literal notranslate"><span class="pre">'&#64;'</span></code> byte order character).
  535. For the standard size, you can use whichever of the other integer formats
  536. fits your application.</p></li>
  537. <li><p>For the <code class="docutils literal notranslate"><span class="pre">'f'</span></code>, <code class="docutils literal notranslate"><span class="pre">'d'</span></code> and <code class="docutils literal notranslate"><span class="pre">'e'</span></code> conversion codes, the packed
  538. representation uses the IEEE 754 binary32, binary64 or binary16 format (for
  539. <code class="docutils literal notranslate"><span class="pre">'f'</span></code>, <code class="docutils literal notranslate"><span class="pre">'d'</span></code> or <code class="docutils literal notranslate"><span class="pre">'e'</span></code> respectively), regardless of the floating-point
  540. format used by the platform.</p></li>
  541. <li><p>The <code class="docutils literal notranslate"><span class="pre">'P'</span></code> format character is only available for the native byte ordering
  542. (selected as the default or with the <code class="docutils literal notranslate"><span class="pre">'&#64;'</span></code> byte order character). The byte
  543. order character <code class="docutils literal notranslate"><span class="pre">'='</span></code> chooses to use little- or big-endian ordering based
  544. on the host system. The struct module does not interpret this as native
  545. ordering, so the <code class="docutils literal notranslate"><span class="pre">'P'</span></code> format is not available.</p></li>
  546. <li><p>The IEEE 754 binary16 “half precision” type was introduced in the 2008
  547. revision of the <a class="reference external" href="https://en.wikipedia.org/wiki/IEEE_754-2008_revision">IEEE 754 standard</a>. It has a sign
  548. bit, a 5-bit exponent and 11-bit precision (with 10 bits explicitly stored),
  549. and can represent numbers between approximately <code class="docutils literal notranslate"><span class="pre">6.1e-05</span></code> and <code class="docutils literal notranslate"><span class="pre">6.5e+04</span></code>
  550. at full precision. This type is not widely supported by C compilers: on a
  551. typical machine, an unsigned short can be used for storage, but not for math
  552. operations. See the Wikipedia page on the <a class="reference external" href="https://en.wikipedia.org/wiki/Half-precision_floating-point_format">half-precision floating-point
  553. format</a> for more information.</p></li>
  554. <li><p>When packing, <code class="docutils literal notranslate"><span class="pre">'x'</span></code> inserts one NUL byte.</p></li>
  555. <li><p>The <code class="docutils literal notranslate"><span class="pre">'p'</span></code> format character encodes a “Pascal string”, meaning a short
  556. variable-length string stored in a <em>fixed number of bytes</em>, given by the count.
  557. The first byte stored is the length of the string, or 255, whichever is
  558. smaller. The bytes of the string follow. If the string passed in to
  559. <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> is too long (longer than the count minus 1), only the leading
  560. <code class="docutils literal notranslate"><span class="pre">count-1</span></code> bytes of the string are stored. If the string is shorter than
  561. <code class="docutils literal notranslate"><span class="pre">count-1</span></code>, it is padded with null bytes so that exactly count bytes in all
  562. are used. Note that for <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>, the <code class="docutils literal notranslate"><span class="pre">'p'</span></code> format character consumes
  563. <code class="docutils literal notranslate"><span class="pre">count</span></code> bytes, but that the string returned can never contain more than 255
  564. bytes.</p></li>
  565. <li><p>For the <code class="docutils literal notranslate"><span class="pre">'s'</span></code> format character, the count is interpreted as the length of the
  566. bytes, not a repeat count like for the other format characters; for example,
  567. <code class="docutils literal notranslate"><span class="pre">'10s'</span></code> means a single 10-byte string mapping to or from a single
  568. Python byte string, while <code class="docutils literal notranslate"><span class="pre">'10c'</span></code> means 10
  569. separate one byte character elements (e.g., <code class="docutils literal notranslate"><span class="pre">cccccccccc</span></code>) mapping
  570. to or from ten different Python byte objects. (See <a class="reference internal" href="#struct-examples"><span class="std std-ref">Examples</span></a>
  571. for a concrete demonstration of the difference.)
  572. If a count is not given, it defaults to 1. For packing, the string is
  573. truncated or padded with null bytes as appropriate to make it fit. For
  574. unpacking, the resulting bytes object always has exactly the specified number
  575. of bytes. As a special case, <code class="docutils literal notranslate"><span class="pre">'0s'</span></code> means a single, empty string (while
  576. <code class="docutils literal notranslate"><span class="pre">'0c'</span></code> means 0 characters).</p></li>
  577. </ol>
  578. <p>A format character may be preceded by an integral repeat count. For example,
  579. the format string <code class="docutils literal notranslate"><span class="pre">'4h'</span></code> means exactly the same as <code class="docutils literal notranslate"><span class="pre">'hhhh'</span></code>.</p>
  580. <p>Whitespace characters between formats are ignored; a count and its format must
  581. not contain whitespace though.</p>
  582. <p>When packing a value <code class="docutils literal notranslate"><span class="pre">x</span></code> using one of the integer formats (<code class="docutils literal notranslate"><span class="pre">'b'</span></code>,
  583. <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>,
  584. <code class="docutils literal notranslate"><span class="pre">'q'</span></code>, <code class="docutils literal notranslate"><span class="pre">'Q'</span></code>), if <code class="docutils literal notranslate"><span class="pre">x</span></code> is outside the valid range for that format
  585. then <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> is raised.</p>
  586. <div class="versionchanged">
  587. <p><span class="versionmodified changed">Changed in version 3.1: </span>Previously, some of the integer formats wrapped out-of-range values and
  588. raised <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> instead of <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>
  589. </div>
  590. <p id="index-3">For the <code class="docutils literal notranslate"><span class="pre">'?'</span></code> format character, the return value is either <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> or
  591. <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>. When packing, the truth value of the argument object is used.
  592. Either 0 or 1 in the native or standard bool representation will be packed, and
  593. any non-zero value will be <code class="docutils literal notranslate"><span class="pre">True</span></code> when unpacking.</p>
  594. </section>
  595. <section id="examples">
  596. <span id="struct-examples"></span><h3>Examples<a class="headerlink" href="#examples" title="Link to this heading">¶</a></h3>
  597. <div class="admonition note">
  598. <p class="admonition-title">Note</p>
  599. <p>Native byte order examples (designated by the <code class="docutils literal notranslate"><span class="pre">'&#64;'</span></code> format prefix or
  600. lack of any prefix character) may not match what the reader’s
  601. machine produces as
  602. that depends on the platform and compiler.</p>
  603. </div>
  604. <p>Pack and unpack integers of three different sizes, using big endian
  605. ordering:</p>
  606. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">struct</span> <span class="kn">import</span> <span class="o">*</span>
  607. <span class="gp">&gt;&gt;&gt; </span><span class="n">pack</span><span class="p">(</span><span class="s2">&quot;&gt;bhl&quot;</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>
  608. <span class="go">b&#39;\x01\x00\x02\x00\x00\x00\x03&#39;</span>
  609. <span class="gp">&gt;&gt;&gt; </span><span class="n">unpack</span><span class="p">(</span><span class="s1">&#39;&gt;bhl&#39;</span><span class="p">,</span> <span class="sa">b</span><span class="s1">&#39;</span><span class="se">\x01\x00\x02\x00\x00\x00\x03</span><span class="s1">&#39;</span><span class="p">)</span>
  610. <span class="go">(1, 2, 3)</span>
  611. <span class="gp">&gt;&gt;&gt; </span><span class="n">calcsize</span><span class="p">(</span><span class="s1">&#39;&gt;bhl&#39;</span><span class="p">)</span>
  612. <span class="go">7</span>
  613. </pre></div>
  614. </div>
  615. <p>Attempt to pack an integer which is too large for the defined field:</p>
  616. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">pack</span><span class="p">(</span><span class="s2">&quot;&gt;h&quot;</span><span class="p">,</span> <span class="mi">99999</span><span class="p">)</span>
  617. <span class="gt">Traceback (most recent call last):</span>
  618. File <span class="nb">&quot;&lt;stdin&gt;&quot;</span>, line <span class="m">1</span>, in <span class="n">&lt;module&gt;</span>
  619. <span class="gr">struct.error</span>: <span class="n">&#39;h&#39; format requires -32768 &lt;= number &lt;= 32767</span>
  620. </pre></div>
  621. </div>
  622. <p>Demonstrate the difference between <code class="docutils literal notranslate"><span class="pre">'s'</span></code> and <code class="docutils literal notranslate"><span class="pre">'c'</span></code> format
  623. characters:</p>
  624. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">pack</span><span class="p">(</span><span class="s2">&quot;@ccc&quot;</span><span class="p">,</span> <span class="sa">b</span><span class="s1">&#39;1&#39;</span><span class="p">,</span> <span class="sa">b</span><span class="s1">&#39;2&#39;</span><span class="p">,</span> <span class="sa">b</span><span class="s1">&#39;3&#39;</span><span class="p">)</span>
  625. <span class="go">b&#39;123&#39;</span>
  626. <span class="gp">&gt;&gt;&gt; </span><span class="n">pack</span><span class="p">(</span><span class="s2">&quot;@3s&quot;</span><span class="p">,</span> <span class="sa">b</span><span class="s1">&#39;123&#39;</span><span class="p">)</span>
  627. <span class="go">b&#39;123&#39;</span>
  628. </pre></div>
  629. </div>
  630. <p>Unpacked fields can be named by assigning them to variables or by wrapping
  631. the result in a named tuple:</p>
  632. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">record</span> <span class="o">=</span> <span class="sa">b</span><span class="s1">&#39;raymond </span><span class="se">\x32\x12\x08\x01\x08</span><span class="s1">&#39;</span>
  633. <span class="gp">&gt;&gt;&gt; </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">&#39;&lt;10sHHb&#39;</span><span class="p">,</span> <span class="n">record</span><span class="p">)</span>
  634. <span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">collections</span> <span class="kn">import</span> <span class="n">namedtuple</span>
  635. <span class="gp">&gt;&gt;&gt; </span><span class="n">Student</span> <span class="o">=</span> <span class="n">namedtuple</span><span class="p">(</span><span class="s1">&#39;Student&#39;</span><span class="p">,</span> <span class="s1">&#39;name serialnum school gradelevel&#39;</span><span class="p">)</span>
  636. <span class="gp">&gt;&gt;&gt; </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">&#39;&lt;10sHHb&#39;</span><span class="p">,</span> <span class="n">record</span><span class="p">))</span>
  637. <span class="go">Student(name=b&#39;raymond &#39;, serialnum=4658, school=264, gradelevel=8)</span>
  638. </pre></div>
  639. </div>
  640. <p>The ordering of format characters may have an impact on size in native
  641. mode since padding is implicit. In standard mode, the user is
  642. responsible for inserting any desired padding.
  643. Note in
  644. the first <code class="docutils literal notranslate"><span class="pre">pack</span></code> call below that three NUL bytes were added after the
  645. packed <code class="docutils literal notranslate"><span class="pre">'#'</span></code> to align the following integer on a four-byte boundary.
  646. In this example, the output was produced on a little endian machine:</p>
  647. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">pack</span><span class="p">(</span><span class="s1">&#39;@ci&#39;</span><span class="p">,</span> <span class="sa">b</span><span class="s1">&#39;#&#39;</span><span class="p">,</span> <span class="mh">0x12131415</span><span class="p">)</span>
  648. <span class="go">b&#39;#\x00\x00\x00\x15\x14\x13\x12&#39;</span>
  649. <span class="gp">&gt;&gt;&gt; </span><span class="n">pack</span><span class="p">(</span><span class="s1">&#39;@ic&#39;</span><span class="p">,</span> <span class="mh">0x12131415</span><span class="p">,</span> <span class="sa">b</span><span class="s1">&#39;#&#39;</span><span class="p">)</span>
  650. <span class="go">b&#39;\x15\x14\x13\x12#&#39;</span>
  651. <span class="gp">&gt;&gt;&gt; </span><span class="n">calcsize</span><span class="p">(</span><span class="s1">&#39;@ci&#39;</span><span class="p">)</span>
  652. <span class="go">8</span>
  653. <span class="gp">&gt;&gt;&gt; </span><span class="n">calcsize</span><span class="p">(</span><span class="s1">&#39;@ic&#39;</span><span class="p">)</span>
  654. <span class="go">5</span>
  655. </pre></div>
  656. </div>
  657. <p>The following format <code class="docutils literal notranslate"><span class="pre">'llh0l'</span></code> results in two pad bytes being added
  658. at the end, assuming the platform’s longs are aligned on 4-byte boundaries:</p>
  659. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">pack</span><span class="p">(</span><span class="s1">&#39;@llh0l&#39;</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>
  660. <span class="go">b&#39;\x00\x00\x00\x01\x00\x00\x00\x02\x00\x03\x00\x00&#39;</span>
  661. </pre></div>
  662. </div>
  663. <div class="admonition seealso">
  664. <p class="admonition-title">See also</p>
  665. <dl class="simple">
  666. <dt>Module <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>Packed binary storage of homogeneous data.</p>
  667. </dd>
  668. <dt>Module <a class="reference internal" href="json.html#module-json" title="json: Encode and decode the JSON format."><code class="xref py py-mod docutils literal notranslate"><span class="pre">json</span></code></a></dt><dd><p>JSON encoder and decoder.</p>
  669. </dd>
  670. <dt>Module <a class="reference internal" href="pickle.html#module-pickle" title="pickle: Convert Python objects to streams of bytes and back."><code class="xref py py-mod docutils literal notranslate"><span class="pre">pickle</span></code></a></dt><dd><p>Python object serialization.</p>
  671. </dd>
  672. </dl>
  673. </div>
  674. </section>
  675. </section>
  676. <section id="applications">
  677. <span id="id2"></span><h2>Applications<a class="headerlink" href="#applications" title="Link to this heading">¶</a></h2>
  678. <p>Two main applications for the <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> module exist, data
  679. interchange between Python and C code within an application or another
  680. application compiled using the same compiler (<a class="reference internal" href="#struct-native-formats"><span class="std std-ref">native formats</span></a>), and
  681. data interchange between applications using agreed upon data layout
  682. (<a class="reference internal" href="#struct-standard-formats"><span class="std std-ref">standard formats</span></a>). Generally speaking, the format strings
  683. constructed for these two domains are distinct.</p>
  684. <section id="native-formats">
  685. <span id="struct-native-formats"></span><h3>Native Formats<a class="headerlink" href="#native-formats" title="Link to this heading">¶</a></h3>
  686. <p>When constructing format strings which mimic native layouts, the
  687. compiler and machine architecture determine byte ordering and padding.
  688. In such cases, the <code class="docutils literal notranslate"><span class="pre">&#64;</span></code> format character should be used to specify
  689. native byte ordering and data sizes. Internal pad bytes are normally inserted
  690. automatically. It is possible that a zero-repeat format code will be
  691. needed at the end of a format string to round up to the correct
  692. byte boundary for proper alignment of consecutive chunks of data.</p>
  693. <p>Consider these two simple examples (on a 64-bit, little-endian
  694. machine):</p>
  695. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">calcsize</span><span class="p">(</span><span class="s1">&#39;@lhl&#39;</span><span class="p">)</span>
  696. <span class="go">24</span>
  697. <span class="gp">&gt;&gt;&gt; </span><span class="n">calcsize</span><span class="p">(</span><span class="s1">&#39;@llh&#39;</span><span class="p">)</span>
  698. <span class="go">18</span>
  699. </pre></div>
  700. </div>
  701. <p>Data is not padded to an 8-byte boundary at the end of the second
  702. format string without the use of extra padding. A zero-repeat format
  703. code solves that problem:</p>
  704. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">calcsize</span><span class="p">(</span><span class="s1">&#39;@llh0l&#39;</span><span class="p">)</span>
  705. <span class="go">24</span>
  706. </pre></div>
  707. </div>
  708. <p>The <code class="docutils literal notranslate"><span class="pre">'x'</span></code> format code can be used to specify the repeat, but for
  709. native formats it is better to use a zero-repeat format like <code class="docutils literal notranslate"><span class="pre">'0l'</span></code>.</p>
  710. <p>By default, native byte ordering and alignment is used, but it is
  711. better to be explicit and use the <code class="docutils literal notranslate"><span class="pre">'&#64;'</span></code> prefix character.</p>
  712. </section>
  713. <section id="standard-formats">
  714. <span id="struct-standard-formats"></span><h3>Standard Formats<a class="headerlink" href="#standard-formats" title="Link to this heading">¶</a></h3>
  715. <p>When exchanging data beyond your process such as networking or storage,
  716. be precise. Specify the exact byte order, size, and alignment. Do
  717. not assume they match the native order of a particular machine.
  718. For example, network byte order is big-endian, while many popular CPUs
  719. are little-endian. By defining this explicitly, the user need not
  720. care about the specifics of the platform their code is running on.
  721. The first character should typically be <code class="docutils literal notranslate"><span class="pre">&lt;</span></code> or <code class="docutils literal notranslate"><span class="pre">&gt;</span></code>
  722. (or <code class="docutils literal notranslate"><span class="pre">!</span></code>). Padding is the responsibility of the programmer. The
  723. zero-repeat format character won’t work. Instead, the user must
  724. explicitly add <code class="docutils literal notranslate"><span class="pre">'x'</span></code> pad bytes where needed. Revisiting the
  725. examples from the previous section, we have:</p>
  726. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">calcsize</span><span class="p">(</span><span class="s1">&#39;&lt;qh6xq&#39;</span><span class="p">)</span>
  727. <span class="go">24</span>
  728. <span class="gp">&gt;&gt;&gt; </span><span class="n">pack</span><span class="p">(</span><span class="s1">&#39;&lt;qh6xq&#39;</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="o">==</span> <span class="n">pack</span><span class="p">(</span><span class="s1">&#39;@lhl&#39;</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>
  729. <span class="go">True</span>
  730. <span class="gp">&gt;&gt;&gt; </span><span class="n">calcsize</span><span class="p">(</span><span class="s1">&#39;@llh&#39;</span><span class="p">)</span>
  731. <span class="go">18</span>
  732. <span class="gp">&gt;&gt;&gt; </span><span class="n">pack</span><span class="p">(</span><span class="s1">&#39;@llh&#39;</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="o">==</span> <span class="n">pack</span><span class="p">(</span><span class="s1">&#39;&lt;qqh&#39;</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>
  733. <span class="go">True</span>
  734. <span class="gp">&gt;&gt;&gt; </span><span class="n">calcsize</span><span class="p">(</span><span class="s1">&#39;&lt;qqh6x&#39;</span><span class="p">)</span>
  735. <span class="go">24</span>
  736. <span class="gp">&gt;&gt;&gt; </span><span class="n">calcsize</span><span class="p">(</span><span class="s1">&#39;@llh0l&#39;</span><span class="p">)</span>
  737. <span class="go">24</span>
  738. <span class="gp">&gt;&gt;&gt; </span><span class="n">pack</span><span class="p">(</span><span class="s1">&#39;@llh0l&#39;</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="o">==</span> <span class="n">pack</span><span class="p">(</span><span class="s1">&#39;&lt;qqh6x&#39;</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>
  739. <span class="go">True</span>
  740. </pre></div>
  741. </div>
  742. <p>The above results (executed on a 64-bit machine) aren’t guaranteed to
  743. match when executed on different machines. For example, the examples
  744. below were executed on a 32-bit machine:</p>
  745. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">calcsize</span><span class="p">(</span><span class="s1">&#39;&lt;qqh6x&#39;</span><span class="p">)</span>
  746. <span class="go">24</span>
  747. <span class="gp">&gt;&gt;&gt; </span><span class="n">calcsize</span><span class="p">(</span><span class="s1">&#39;@llh0l&#39;</span><span class="p">)</span>
  748. <span class="go">12</span>
  749. <span class="gp">&gt;&gt;&gt; </span><span class="n">pack</span><span class="p">(</span><span class="s1">&#39;@llh0l&#39;</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="o">==</span> <span class="n">pack</span><span class="p">(</span><span class="s1">&#39;&lt;qqh6x&#39;</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>
  750. <span class="go">False</span>
  751. </pre></div>
  752. </div>
  753. </section>
  754. </section>
  755. <section id="classes">
  756. <span id="struct-objects"></span><h2>Classes<a class="headerlink" href="#classes" title="Link to this heading">¶</a></h2>
  757. <p>The <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> module also defines the following type:</p>
  758. <dl class="py class">
  759. <dt class="sig sig-object py" id="struct.Struct">
  760. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">struct.</span></span><span class="sig-name descname"><span class="pre">Struct</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">format</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#struct.Struct" title="Link to this definition">¶</a></dt>
  761. <dd><p>Return a new Struct object which writes and reads binary data according to
  762. the format string <em>format</em>. Creating a <code class="docutils literal notranslate"><span class="pre">Struct</span></code> object once and calling its
  763. methods is more efficient than calling module-level functions with the
  764. same format since the format string is only compiled once.</p>
  765. <div class="admonition note">
  766. <p class="admonition-title">Note</p>
  767. <p>The compiled versions of the most recent format strings passed to
  768. the module-level functions are cached, so programs that use only a few
  769. format strings needn’t worry about reusing a single <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>
  770. instance.</p>
  771. </div>
  772. <p>Compiled Struct objects support the following methods and attributes:</p>
  773. <dl class="py method">
  774. <dt class="sig sig-object py" id="struct.Struct.pack">
  775. <span class="sig-name descname"><span class="pre">pack</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">v1</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">v2</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">...</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#struct.Struct.pack" title="Link to this definition">¶</a></dt>
  776. <dd><p>Identical to the <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> function, using the compiled format.
  777. (<code class="docutils literal notranslate"><span class="pre">len(result)</span></code> will equal <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>
  778. </dd></dl>
  779. <dl class="py method">
  780. <dt class="sig sig-object py" id="struct.Struct.pack_into">
  781. <span class="sig-name descname"><span class="pre">pack_into</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">buffer</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">offset</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">v1</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">v2</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">...</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#struct.Struct.pack_into" title="Link to this definition">¶</a></dt>
  782. <dd><p>Identical to the <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> function, using the compiled format.</p>
  783. </dd></dl>
  784. <dl class="py method">
  785. <dt class="sig sig-object py" id="struct.Struct.unpack">
  786. <span class="sig-name descname"><span class="pre">unpack</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">buffer</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#struct.Struct.unpack" title="Link to this definition">¶</a></dt>
  787. <dd><p>Identical to the <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> function, using the compiled format.
  788. The buffer’s size in bytes must equal <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>
  789. </dd></dl>
  790. <dl class="py method">
  791. <dt class="sig sig-object py" id="struct.Struct.unpack_from">
  792. <span class="sig-name descname"><span class="pre">unpack_from</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">buffer</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">offset</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">0</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#struct.Struct.unpack_from" title="Link to this definition">¶</a></dt>
  793. <dd><p>Identical to the <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> function, using the compiled format.
  794. The buffer’s size in bytes, starting at position <em>offset</em>, must be at least
  795. <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>
  796. </dd></dl>
  797. <dl class="py method">
  798. <dt class="sig sig-object py" id="struct.Struct.iter_unpack">
  799. <span class="sig-name descname"><span class="pre">iter_unpack</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">buffer</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#struct.Struct.iter_unpack" title="Link to this definition">¶</a></dt>
  800. <dd><p>Identical to the <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> function, using the compiled format.
  801. The buffer’s size in bytes must be a multiple of <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>
  802. <div class="versionadded">
  803. <p><span class="versionmodified added">New in version 3.4.</span></p>
  804. </div>
  805. </dd></dl>
  806. <dl class="py attribute">
  807. <dt class="sig sig-object py" id="struct.Struct.format">
  808. <span class="sig-name descname"><span class="pre">format</span></span><a class="headerlink" href="#struct.Struct.format" title="Link to this definition">¶</a></dt>
  809. <dd><p>The format string used to construct this Struct object.</p>
  810. <div class="versionchanged">
  811. <p><span class="versionmodified changed">Changed in version 3.7: </span>The format string type is now <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> instead of <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>
  812. </div>
  813. </dd></dl>
  814. <dl class="py attribute">
  815. <dt class="sig sig-object py" id="struct.Struct.size">
  816. <span class="sig-name descname"><span class="pre">size</span></span><a class="headerlink" href="#struct.Struct.size" title="Link to this definition">¶</a></dt>
  817. <dd><p>The calculated size of the struct (and hence of the bytes object produced
  818. by the <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> method) corresponding to <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>.</p>
  819. </dd></dl>
  820. </dd></dl>
  821. </section>
  822. </section>
  823. <div class="clearer"></div>
  824. </div>
  825. </div>
  826. </div>
  827. <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
  828. <div class="sphinxsidebarwrapper">
  829. <div>
  830. <h3><a href="../contents.html">Table of Contents</a></h3>
  831. <ul>
  832. <li><a class="reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">struct</span></code> — Interpret bytes as packed binary data</a><ul>
  833. <li><a class="reference internal" href="#functions-and-exceptions">Functions and Exceptions</a></li>
  834. <li><a class="reference internal" href="#format-strings">Format Strings</a><ul>
  835. <li><a class="reference internal" href="#byte-order-size-and-alignment">Byte Order, Size, and Alignment</a></li>
  836. <li><a class="reference internal" href="#format-characters">Format Characters</a></li>
  837. <li><a class="reference internal" href="#examples">Examples</a></li>
  838. </ul>
  839. </li>
  840. <li><a class="reference internal" href="#applications">Applications</a><ul>
  841. <li><a class="reference internal" href="#native-formats">Native Formats</a></li>
  842. <li><a class="reference internal" href="#standard-formats">Standard Formats</a></li>
  843. </ul>
  844. </li>
  845. <li><a class="reference internal" href="#classes">Classes</a></li>
  846. </ul>
  847. </li>
  848. </ul>
  849. </div>
  850. <div>
  851. <h4>Previous topic</h4>
  852. <p class="topless"><a href="binary.html"
  853. title="previous chapter">Binary Data Services</a></p>
  854. </div>
  855. <div>
  856. <h4>Next topic</h4>
  857. <p class="topless"><a href="codecs.html"
  858. title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">codecs</span></code> — Codec registry and base classes</a></p>
  859. </div>
  860. <div role="note" aria-label="source link">
  861. <h3>This Page</h3>
  862. <ul class="this-page-menu">
  863. <li><a href="../bugs.html">Report a Bug</a></li>
  864. <li>
  865. <a href="https://github.com/python/cpython/blob/main/Doc/library/struct.rst"
  866. rel="nofollow">Show Source
  867. </a>
  868. </li>
  869. </ul>
  870. </div>
  871. </div>
  872. <div id="sidebarbutton" title="Collapse sidebar">
  873. <span>«</span>
  874. </div>
  875. </div>
  876. <div class="clearer"></div>
  877. </div>
  878. <div class="related" role="navigation" aria-label="related navigation">
  879. <h3>Navigation</h3>
  880. <ul>
  881. <li class="right" style="margin-right: 10px">
  882. <a href="../genindex.html" title="General Index"
  883. >index</a></li>
  884. <li class="right" >
  885. <a href="../py-modindex.html" title="Python Module Index"
  886. >modules</a> |</li>
  887. <li class="right" >
  888. <a href="codecs.html" title="codecs — Codec registry and base classes"
  889. >next</a> |</li>
  890. <li class="right" >
  891. <a href="binary.html" title="Binary Data Services"
  892. >previous</a> |</li>
  893. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  894. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  895. <li class="switchers">
  896. <div class="language_switcher_placeholder"></div>
  897. <div class="version_switcher_placeholder"></div>
  898. </li>
  899. <li>
  900. </li>
  901. <li id="cpython-language-and-version">
  902. <a href="../index.html">3.12.3 Documentation</a> &#187;
  903. </li>
  904. <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
  905. <li class="nav-item nav-item-2"><a href="binary.html" >Binary Data Services</a> &#187;</li>
  906. <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">struct</span></code> — Interpret bytes as packed binary data</a></li>
  907. <li class="right">
  908. <div class="inline-search" role="search">
  909. <form class="inline-search" action="../search.html" method="get">
  910. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  911. <input type="submit" value="Go" />
  912. </form>
  913. </div>
  914. |
  915. </li>
  916. <li class="right">
  917. <label class="theme-selector-label">
  918. Theme
  919. <select class="theme-selector" oninput="activateTheme(this.value)">
  920. <option value="auto" selected>Auto</option>
  921. <option value="light">Light</option>
  922. <option value="dark">Dark</option>
  923. </select>
  924. </label> |</li>
  925. </ul>
  926. </div>
  927. <div class="footer">
  928. &copy;
  929. <a href="../copyright.html">
  930. Copyright
  931. </a>
  932. 2001-2024, Python Software Foundation.
  933. <br />
  934. This page is licensed under the Python Software Foundation License Version 2.
  935. <br />
  936. Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.
  937. <br />
  938. See <a href="/license.html">History and License</a> for more information.<br />
  939. <br />
  940. The Python Software Foundation is a non-profit corporation.
  941. <a href="https://www.python.org/psf/donations/">Please donate.</a>
  942. <br />
  943. <br />
  944. Last updated on Apr 09, 2024 (13:47 UTC).
  945. <a href="/bugs.html">Found a bug</a>?
  946. <br />
  947. Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 7.2.6.
  948. </div>
  949. </body>
  950. </html>
上海开阖软件有限公司 沪ICP备12045867号-1