⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 module-struct.html

📁 一本很好的python的说明书,适合对python感兴趣的人
💻 HTML
📖 第 1 页 / 共 2 页
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>4.3 struct -- Interpret strings as packed binary data</title>
<META NAME="description" CONTENT="4.3 struct -- Interpret strings as packed binary data">
<META NAME="keywords" CONTENT="lib">
<META NAME="resource-type" CONTENT="document">
<META NAME="distribution" CONTENT="global">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link rel="STYLESHEET" href="lib.css" tppabs="http://www.python.org/doc/current/lib/lib.css">
<LINK REL="next" href="module-fpformat.html" tppabs="http://www.python.org/doc/current/lib/module-fpformat.html">
<LINK REL="previous" href="module-re.html" tppabs="http://www.python.org/doc/current/lib/module-re.html">
<LINK REL="up" href="strings.html" tppabs="http://www.python.org/doc/current/lib/strings.html">
<LINK REL="next" href="module-fpformat.html" tppabs="http://www.python.org/doc/current/lib/module-fpformat.html">
</head>
<body>
<DIV CLASS="navigation"><table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td><A href="match-objects.html" tppabs="http://www.python.org/doc/current/lib/match-objects.html"><img src="previous.gif" tppabs="http://www.python.org/doc/current/icons/previous.gif" border="0" height="32"
  alt="Previous Page" width="32"></A></td>
<td><A href="strings.html" tppabs="http://www.python.org/doc/current/lib/strings.html"><img src="up.gif" tppabs="http://www.python.org/doc/current/icons/up.gif" border="0" height="32"
  alt="Up One Level" width="32"></A></td>
<td><A href="module-fpformat.html" tppabs="http://www.python.org/doc/current/lib/module-fpformat.html"><img src="next.gif" tppabs="http://www.python.org/doc/current/icons/next.gif" border="0" height="32"
  alt="Next Page" width="32"></A></td>
<td align="center" width="100%">Python Library Reference</td>
<td><A href="contents.html" tppabs="http://www.python.org/doc/current/lib/contents.html"><img src="contents.gif" tppabs="http://www.python.org/doc/current/icons/contents.gif" border="0" height="32"
  alt="Contents" width="32"></A></td>
<td><a href="modindex.html" tppabs="http://www.python.org/doc/current/lib/modindex.html" title="Module Index"><img src="modules.gif" tppabs="http://www.python.org/doc/current/icons/modules.gif" border="0" height="32"
  alt="Module Index" width="32"></a></td>
<td><A href="genindex.html" tppabs="http://www.python.org/doc/current/lib/genindex.html"><img src="index.gif" tppabs="http://www.python.org/doc/current/icons/index.gif" border="0" height="32"
  alt="Index" width="32"></A></td>
</tr></table>
<b class="navlabel">Previous:</b> <a class="sectref" href="match-objects.html" tppabs="http://www.python.org/doc/current/lib/match-objects.html">4.2.5 Match Objects</A>
<b class="navlabel">Up:</b> <a class="sectref" href="strings.html" tppabs="http://www.python.org/doc/current/lib/strings.html">4. String Services</A>
<b class="navlabel">Next:</b> <a class="sectref" href="module-fpformat.html" tppabs="http://www.python.org/doc/current/lib/module-fpformat.html">4.4 fpformat  </A>
<br><hr></DIV>
<!--End of Navigation Panel-->

<H1><A NAME="SECTION006300000000000000000">
4.3 <tt class="module">struct</tt> --
         Interpret strings as packed binary data</A>
</H1>

<P>

<P>


<P>
This module performs conversions between Python values and C
structs represented as Python strings.  It uses <i class="dfn">format strings</i>
(explained below) as compact descriptions of the lay-out of the C
structs and the intended conversion to/from Python values.  This can
be used in handling binary data stored in files or from network
connections, among other sources.

<P>
The module defines the following exception and functions:

<P>
<dl><dt><b><a name='l2h-632'><tt class='exception'>error</tt></a></b>
<dd>
  Exception raised on various occasions; argument is a string
  describing what is wrong.
</dl>

<P>
<dl><dt><b><a name='l2h-633'><tt class='function'>pack</tt></a></b> (<var>fmt, v1, v2,  ...</var>)
<dd>
  Return a string containing the values
  <code><var>v1</var>, <var>v2</var>,  ...</code> packed according to the given
  format.  The arguments must match the values required by the format
  exactly.
</dl>

<P>
<dl><dt><b><a name='l2h-634'><tt class='function'>unpack</tt></a></b> (<var>fmt, string</var>)
<dd>
  Unpack the string (presumably packed by <code>pack(<var>fmt</var>,
   ...)</code>) according to the given format.  The result is a
  tuple even if it contains exactly one item.  The string must contain
  exactly the amount of data required by the format (i.e.
  <code>len(<var>string</var>)</code> must equal <code>calcsize(<var>fmt</var>)</code>).
</dl>

<P>
<dl><dt><b><a name='l2h-635'><tt class='function'>calcsize</tt></a></b> (<var>fmt</var>)
<dd>
  Return the size of the struct (and hence of the string)
  corresponding to the given format.
</dl>

<P>
Format characters have the following meaning; the conversion between
C and Python values should be obvious given their types:

<P>
<table border align="center" style="border-collapse: collapse">
  <thead>
    <tr>
      <th align="center"><b>Format</b>&nbsp;</th>
      <th align="left"><b>C Type</b>&nbsp;</th>
      <th align="left"><b>Python</b>&nbsp;</th>
      <th align="center"><b>Notes</b>&nbsp;</th>
    </thead>
  <tbody valign='baseline'>
    <tr><td align="center" valign="baseline"><samp>x</samp></td>
        <td align="left">pad byte</td>
        <td align="left">no value</td>
        <td align="center">&nbsp;</td>
    <tr><td align="center" valign="baseline"><samp>c</samp></td>
        <td align="left"><tt class="ctype">char</tt></td>
        <td align="left">string of length 1</td>
        <td align="center">&nbsp;</td>
    <tr><td align="center" valign="baseline"><samp>b</samp></td>
        <td align="left"><tt class="ctype">signed char</tt></td>
        <td align="left">integer</td>
        <td align="center">&nbsp;</td>
    <tr><td align="center" valign="baseline"><samp>B</samp></td>
        <td align="left"><tt class="ctype">unsigned char</tt></td>
        <td align="left">integer</td>
        <td align="center">&nbsp;</td>
    <tr><td align="center" valign="baseline"><samp>h</samp></td>
        <td align="left"><tt class="ctype">short</tt></td>
        <td align="left">integer</td>
        <td align="center">&nbsp;</td>
    <tr><td align="center" valign="baseline"><samp>H</samp></td>
        <td align="left"><tt class="ctype">unsigned short</tt></td>
        <td align="left">integer</td>
        <td align="center">&nbsp;</td>
    <tr><td align="center" valign="baseline"><samp>i</samp></td>
        <td align="left"><tt class="ctype">int</tt></td>
        <td align="left">integer</td>
        <td align="center">&nbsp;</td>
    <tr><td align="center" valign="baseline"><samp>I</samp></td>
        <td align="left"><tt class="ctype">unsigned int</tt></td>
        <td align="left">long</td>
        <td align="center">(1)</td>
    <tr><td align="center" valign="baseline"><samp>l</samp></td>
        <td align="left"><tt class="ctype">long</tt></td>
        <td align="left">integer</td>
        <td align="center">&nbsp;</td>
    <tr><td align="center" valign="baseline"><samp>L</samp></td>
        <td align="left"><tt class="ctype">unsigned long</tt></td>
        <td align="left">long</td>
        <td align="center">&nbsp;</td>
    <tr><td align="center" valign="baseline"><samp>f</samp></td>
        <td align="left"><tt class="ctype">float</tt></td>
        <td align="left">float</td>
        <td align="center">&nbsp;</td>
    <tr><td align="center" valign="baseline"><samp>d</samp></td>
        <td align="left"><tt class="ctype">double</tt></td>
        <td align="left">float</td>
        <td align="center">&nbsp;</td>
    <tr><td align="center" valign="baseline"><samp>s</samp></td>
        <td align="left"><tt class="ctype">char[]</tt></td>
        <td align="left">string</td>
        <td align="center">&nbsp;</td>
    <tr><td align="center" valign="baseline"><samp>p</samp></td>
        <td align="left"><tt class="ctype">char[]</tt></td>
        <td align="left">string</td>
        <td align="center">&nbsp;</td>
    <tr><td align="center" valign="baseline"><samp>P</samp></td>
        <td align="left"><tt class="ctype">void *</tt></td>
        <td align="left">integer</td>
        <td align="center">&nbsp;</td></tbody>
</table>

<P>
Notes:

<P>
<DL>
<DT><STRONG>(1)</STRONG></DT>
<DD>The "<tt class="character">I</tt>" conversion code will convert to a Python long if
  the C <tt class="ctype">int</tt> is the same size as a C <tt class="ctype">long</tt>, which is
  typical on most modern systems.  If a C <tt class="ctype">int</tt> is smaller than
  a C <tt class="ctype">long</tt>, an Python integer will be created instead.
</DD>
</DL>

<P>
A format character may be preceded by an integral repeat count;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -