he struct module includes functions for converting between strings of bytes and native Python data types such as numbers and strings.

Functions vs. Struct Class

There are a set of module-level functions for working with structured values, and there is also the Struct class (new in Python 2.5). Format specifiers are converted from their string format to a compiled representation, similar to the way regular expressions are. The conversion takes some resources, so it is typically more efficient to do it once when creating a Structinstance and call methods on the instance instead of using the module-level functions. All of the examples below use the Struct class.

Packing and Unpacking

Structs support packing data into strings, and unpacking data from strings using format specifiers made up of characters representing the type of the data and optional count and endian-ness indicators. For complete details, refer to the standard library documentation.

In this example, the format specifier calls for an integer or long value, a two character string, and a floating point number. The spaces between the format specifiers are included here for clarity, and are ignored when the format is compiled.

import struct
import binascii values = (1, 'ab', 2.7)
s = struct.Struct('I 2s f')
packed_data = s.pack(*values) print 'Original values:', values
print 'Format string :', s.format
print 'Uses :', s.size, 'bytes'
print 'Packed Value :', binascii.hexlify(packed_data)

The example converts the packed value to a sequence of hex bytes for printing with binascii.hexlify(), since some of the characters are nulls.

$ python struct_pack.py

Original values: (1, 'ab', 2.7)
Format string : I 2s f
Uses : 12 bytes
Packed Value : 0100000061620000cdcc2c40

If we pass the packed value to unpack(), we get basically the same values back (note the discrepancy in the floating point value).

import struct
import binascii packed_data = binascii.unhexlify('0100000061620000cdcc2c40') s = struct.Struct('I 2s f')
unpacked_data = s.unpack(packed_data)
print 'Unpacked Values:', unpacked_data
$ python struct_unpack.py

Unpacked Values: (1, 'ab', 2.700000047683716)

Endianness

By default values are encoded using the native C library notion of “endianness”. It is easy to override that choice by providing an explicit endianness directive in the format string.

import struct
import binascii values = (1, 'ab', 2.7)
print 'Original values:', values endianness = [
('@', 'native, native'),
('=', 'native, standard'),
('<', 'little-endian'),
('>', 'big-endian'),
('!', 'network'),
] for code, name in endianness:
s = struct.Struct(code + ' I 2s f')
packed_data = s.pack(*values)
print
print 'Format string :', s.format, 'for', name
print 'Uses :', s.size, 'bytes'
print 'Packed Value :', binascii.hexlify(packed_data)
print 'Unpacked Value :', s.unpack(packed_data)
$ python struct_endianness.py

Original values: (1, 'ab', 2.7)

Format string  : @ I 2s f for native, native
Uses : 12 bytes
Packed Value : 0100000061620000cdcc2c40
Unpacked Value : (1, 'ab', 2.700000047683716) Format string : = I 2s f for native, standard
Uses : 10 bytes
Packed Value : 010000006162cdcc2c40
Unpacked Value : (1, 'ab', 2.700000047683716) Format string : < I 2s f for little-endian
Uses : 10 bytes
Packed Value : 010000006162cdcc2c40
Unpacked Value : (1, 'ab', 2.700000047683716) Format string : > I 2s f for big-endian
Uses : 10 bytes
Packed Value : 000000016162402ccccd
Unpacked Value : (1, 'ab', 2.700000047683716) Format string : ! I 2s f for network
Uses : 10 bytes
Packed Value : 000000016162402ccccd
Unpacked Value : (1, 'ab', 2.700000047683716)

Buffers

Working with binary packed data is typically reserved for highly performance sensitive situations or passing data into and out of extension modules. In such situations, you can optimize by avoiding the overhead of allocating a new buffer for each packed structure. The pack_into() and unpack_from() methods support writing to pre-allocated buffers directly.

import struct
import binascii s = struct.Struct('I 2s f')
values = (1, 'ab', 2.7)
print 'Original:', values print
print 'ctypes string buffer' import ctypes
b = ctypes.create_string_buffer(s.size)
print 'Before :', binascii.hexlify(b.raw)
s.pack_into(b, 0, *values)
print 'After :', binascii.hexlify(b.raw)
print 'Unpacked:', s.unpack_from(b, 0) print
print 'array' import array
a = array.array('c', '\0' * s.size)
print 'Before :', binascii.hexlify(a)
s.pack_into(a, 0, *values)
print 'After :', binascii.hexlify(a)
print 'Unpacked:', s.unpack_from(a, 0)

The size attribute of the Struct tells us how big the buffer needs to be.

$ python struct_buffers.py

Original: (1, 'ab', 2.7)

ctypes string buffer
Before : 000000000000000000000000
After : 0100000061620000cdcc2c40
Unpacked: (1, 'ab', 2.700000047683716) array
Before : 000000000000000000000000
After : 0100000061620000cdcc2c40
Unpacked: (1, 'ab', 2.700000047683716)

See also

struct
The standard library documentation for this module.
array
The array module, for working with sequences of fixed-type values.
binascii
The binascii module, for producing ASCII representations of binary data.
WikiPedia: Endianness
Explanation of byte order and endianness in encoding.
In-Memory Data Structures
More tools for working with data structures.

import struct

pack、unpack、pack_into、unpack_from

  1. import struct
  2. #pack - unpack
  3. print
  4. print '===== pack - unpack ====='
  5. str = struct.pack("ii", 20, 400)
  6. print 'str:', str
  7. print 'len(str):', len(str) # len(str): 8
  8. a1, a2 = struct.unpack("ii", str)
  9. print "a1:", a1  # a1: 20
  10. print "a2:", a2  # a2: 400
  11. print 'struct.calcsize:', struct.calcsize("ii") # struct.calcsize: 8
  12. #unpack
  13. print
  14. print '===== unpack ====='
  15. string = 'test astring'
  16. format = '5s 4x 3s'
  17. print struct.unpack(format, string) # ('test ', 'ing')
  18. string = 'he is not very happy'
  19. format = '2s 1x 2s 5x 4s 1x 5s'
  20. print struct.unpack(format, string) # ('he', 'is', 'very', 'happy')
  21. #pack
  22. print
  23. print '===== pack ====='
  24. a = 20
  25. b = 400
  26. str = struct.pack("ii", a, b)
  27. print 'length:', len(str) #length: 8
  28. print str
  29. print repr(str) # '/x14/x00/x00/x00/x90/x01/x00/x00'
  30. #pack_into - unpack_from
  31. print
  32. print '===== pack_into - unpack_from ====='
  33. from ctypes import create_string_buffer
  34. buf = create_string_buffer(12)
  35. print repr(buf.raw)
  36. struct.pack_into("iii", buf, 0, 1, 2, -1)
  37. print repr(buf.raw)
  38. print struct.unpack_from("iii", buf, 0)

运行结果:

[work@db-testing-com06-vm3.db01.baidu.com python]$ python struct_pack.py

===== pack - unpack =====
str: ?
len(str): 8
a1: 20
a2: 400
struct.calcsize: 8

===== unpack =====
('test ', 'ing')
('he', 'is', 'very', 'happy')

===== pack =====
length: 8
?
'/x14/x00/x00/x00/x90/x01/x00/x00'

===== pack_into - unpack_from =====
'/x00/x00/x00/x00/x00/x00/x00/x00/x00/x00/x00/x00'
'/x01/x00/x00/x00/x02/x00/x00/x00/xff/xff/xff/xff'
(1, 2, -1)

==============================================================================

Python是一门非常简洁的语言,对于数据类型的表示,不像其他语言预定义了许多类型(如:在C#中,光整型就定义了8种)

它只定义了六种基本类型:字符串,整数,浮点数,元组(set),列表(array),字典(key/value)

通过这六种数据类型,我们可以完成大部分工作。但当Python需要通过网络与其他的平台进行交互的时候,必须考虑到将这些数据类型与其他平台或语言之间的类型进行互相转换问题。打个比方:C++写的客户端发送一个int型(4字节)变量的数据到Python写的服务器,Python接收到表示这个整数的4个字节数据,怎么解析成Python认识的整数呢? Python的标准模块struct就用来解决这个问题。

struct模块的内容不多,也不是太难,下面对其中最常用的方法进行介绍:

1、 struct.pack
struct.pack用于将Python的值根据格式符,转换为字符串(因为Python中没有字节(Byte)类型,可以把这里的字符串理解为字节流,或字节数组)。其函数原型为:struct.pack(fmt, v1, v2, ...),参数fmt是格式字符串,关于格式字符串的相关信息在下面有所介绍。v1, v2, ...表示要转换的python值。下面的例子将两个整数转换为字符串(字节流):

  1. #!/usr/bin/env python
  2. #encoding: utf8
  3. import sys
  4. reload(sys)
  5. sys.setdefaultencoding("utf-8")
  6. import struct
  7. a = 20
  8. b = 400
  9. str = struct.pack("ii", a, b)
  10. print 'length: ', len(str)          # length:  8
  11. print str                           # 乱码:
  12. print repr(str)                     # '\x14\x00\x00\x00\x90\x01\x00\x00'

格式符"i"表示转换为int,'ii'表示有两个int变量。

进行转换后的结果长度为8个字节(int类型占用4个字节,两个int为8个字节)

可以看到输出的结果是乱码,因为结果是二进制数据,所以显示为乱码。

可以使用python的内置函数repr来获取可识别的字符串,其中十六进制的0x00000014, 0x00001009分别表示20和400。

2、 struct.unpack
struct.unpack做的工作刚好与struct.pack相反,用于将字节流转换成python数据类型。它的函数原型为:struct.unpack(fmt, string),该函数返回一个元组。

下面是一个简单的例子:

  1. #!/usr/bin/env python
  2. #encoding: utf8
  3. import sys
  4. reload(sys)
  5. sys.setdefaultencoding("utf-8")
  6. import struct
  7. a = 20
  8. b = 400
  9. # pack
  10. str = struct.pack("ii", a, b)
  11. print 'length: ', len(str)          # length:  8
  12. print str                           # 乱码:
  13. print repr(str)                     # '\x14\x00\x00\x00\x90\x01\x00\x00'
  14. # unpack
  15. str2 = struct.unpack("ii", str)
  16. print 'length: ', len(str2)          # length:  2
  17. print str2                           # (20, 400)
  18. print repr(str2)                     # (20, 400)

3、 struct.calcsize
struct.calcsize用于计算格式字符串所对应的结果的长度,如:struct.calcsize('ii'),返回8。因为两个int类型所占用的长度是8个字节。

  1. import struct
  2. print "len: ", struct.calcsize('i')       # len:  4
  3. print "len: ", struct.calcsize('ii')      # len:  8
  4. print "len: ", struct.calcsize('f')       # len:  4
  5. print "len: ", struct.calcsize('ff')      # len:  8
  6. print "len: ", struct.calcsize('s')       # len:  1
  7. print "len: ", struct.calcsize('ss')      # len:  2
  8. print "len: ", struct.calcsize('d')       # len:  8
  9. print "len: ", struct.calcsize('dd')      # len:  16

4、 struct.pack_into、 struct.unpack_from
这两个函数在Python手册中有所介绍,但没有给出如何使用的例子。其实它们在实际应用中用的并不多。Google了很久,才找到一个例子,贴出来共享一下:

  1. #!/usr/bin/env python
  2. #encoding: utf8
  3. import sys
  4. reload(sys)
  5. sys.setdefaultencoding("utf-8")
  6. import struct
  7. from ctypes import create_string_buffer
  8. buf = create_string_buffer(12)
  9. print repr(buf.raw)     # '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
  10. struct.pack_into("iii", buf, 0, 1, 2, -1)
  11. print repr(buf.raw)     # '\x01\x00\x00\x00\x02\x00\x00\x00\xff\xff\xff\xff'
  12. print struct.unpack_from("iii", buf, 0)     # (1, 2, -1)

具体内容请参考Python手册 struct 模块

Python手册 struct 模块:http://docs.python.org/library/struct.html#module-struct

struct 类型表

Format C Type Python type Standard size Notes
x pad byte no value    
c char string of length 1 1  
b signed char integer 1 (3)
B unsigned char integer 1 (3)
? _Bool bool 1 (1)
h short integer 2 (3)
H unsigned short integer 2 (3)
i int integer 4 (3)
I unsigned int integer 4 (3)
l long integer 4 (3)
L unsigned long integer 4 (3)
q long long integer 8 (2), (3)
Q unsigned long long integer 8 (2), (3)
f float float 4 (4)
d double float 8 (4)
s char[] string 1  
p char[] string    
P void * integer   (5), (3)

Notes:

  1. The '?' conversion code corresponds to the _Bool type defined by C99. If this type is not available, it is simulated using a char. In standard mode, it is always represented by one byte.

    New in version 2.6.

  2. The 'q' and 'Q' conversion codes are available in native mode only if the platform C compiler supports Clong long, or, on Windows, __int64. They are always available in standard modes.

    New in version 2.2.

  3. When attempting to pack a non-integer using any of the integer conversion codes, if the non-integer has a__index__() method then that method is called to convert the argument to an integer before packing. If no__index__() method exists, or the call to __index__() raises TypeError, then the __int__() method is tried. However, the use of __int__() is deprecated, and will raise DeprecationWarning.

    Changed in version 2.7: Use of the __index__() method for non-integers is new in 2.7.

    Changed in version 2.7: Prior to version 2.7, not all integer conversion codes would use the __int__()method to convert, and DeprecationWarning was raised only for float arguments.

  4. For the 'f' and 'd' conversion codes, the packed representation uses the IEEE 754 binary32 (for 'f') or binary64 (for 'd') format, regardless of the floating-point format used by the platform.

  5. The 'P' format character is only available for the native byte ordering (selected as the default or with the '@'byte order character). The byte order character '=' chooses to use little- or big-endian ordering based on the host system. The struct module does not interpret this as native ordering, so the 'P' format is not available.

A format character may be preceded by an integral repeat count. For example, the format string '4h' means exactly the same as 'hhhh'.

Whitespace characters between formats are ignored; a count and its format must not contain whitespace though.

For the 's' format character, the count is interpreted as the size of the string, not a repeat count like for the other format characters; for example, '10s' means a single 10-byte string, while '10c' means 10 characters. For packing, the string is truncated or padded with null bytes as appropriate to make it fit. For unpacking, the resulting string always has exactly the specified number of bytes. As a special case, '0s' means a single, empty string (while '0c' means 0 characters).

The 'p' format character encodes a “Pascal string”, meaning a short variable-length string stored in a fixed number of bytes, given by the count. The first byte stored is the length of the string, or 255, whichever is smaller. The bytes of the string follow. If the string passed in to pack() is too long (longer than the count minus 1), only the leading count-1bytes of the string are stored. If the string is shorter than count-1, it is padded with null bytes so that exactly count bytes in all are used. Note that for unpack(), the 'p' format character consumes count bytes, but that the string returned can never contain more than 255 characters.

For the 'P' format character, the return value is a Python integer or long integer, depending on the size needed to hold a pointer when it has been cast to an integer type. A NULL pointer will always be returned as the Python integer0. When packing pointer-sized values, Python integer or long integer objects may be used. For example, the Alpha and Merced processors use 64-bit pointer values, meaning a Python long integer will be used to hold the pointer; other platforms use 32-bit pointers and will use a Python integer.

For the '?' format character, the return value is either True or False. When packing, the truth value of the argument object is used. Either 0 or 1 in the native or standard bool representation will be packed, and any non-zero value will be True when unpacking.

Python学习——struct模块的pack、unpack示例的更多相关文章

  1. Python:struct模块的pack、unpack

    mport struct pack.unpack.pack_into.unpack_from 1 # ref: http://blog.csdn<a href="http://lib. ...

  2. day30 python学习 struct模块和 subprocess 模块

    import subprocess import struct aa=input('>>') obj=subprocess.Popen(aa,shell=True,#aa代表的是读取字符串 ...

  3. Python使用struct处理二进制(pack和unpack用法)

    转载自:http://www.cnblogs.com/gala/archive/2011/09/22/2184801.html 这篇文章写的很好,所以无耻的转了.. 有的时候需要用python处理二进 ...

  4. python struct中的pack unpack

    python struct中的pack unpack pytyon tuple元组 print struct.unpack("!ihb", buffer)  结果为7 //pyth ...

  5. python中struct模块及packet和unpacket

    转自:http://www.cnblogs.com/gala/archive/2011/09/22/2184801.html 我们知道python只定义了6种数据类型,字符串,整数,浮点数,列表,元组 ...

  6. c语言write与python的struct模块交互

    以下讲的都是用二进制形式打开文件.网上有很多struct模块的文章,下面是我做的小实验. 1.对于c里面的fwrite写入一个单字节,写的就是它的二进制.如3,写入文件就是二进制0x03,它并不是3的 ...

  7. 【转】在Python的struct模块中进行数据格式转换的方法

    这篇文章主要介绍了在Python的struct模块中进行数据格式转换的方法,文中还给出了C语言和Python语言的数据类型比较,需要的朋友可以参考下 Python是一门非常简洁的语言,对于数据类型的表 ...

  8. Python学习--Selenium模块

    1. Python学习--Selenium模块介绍(1) 2.Python学习--Selenium模块学习(2) 其他: 1. Python学习--打码平台

  9. Python学习--Selenium模块学习(2)

    Selenium的基本操作 获取浏览器驱动寻找方式 1. 通过手动指定浏览器驱动路径2. 通过 `$PATH`环境变量找寻浏览器驱动 可参考Python学习--Selenium模块简单介绍(1) 控制 ...

随机推荐

  1. Android基础总结(10)——手机多媒体的运用:通知、短信、相机、视频播放

    Android提供了一系列的API,是我们可以在程序中调用很多手机的多媒体资源,从而编写出更加丰富的应用程序. 1.通知的使用 通知(Notification)是Android中比较有特色的一个功能, ...

  2. Response.Write页面跳转

    一.<a>标签 <a href=”test.aspx”></a> 这是最常见的一种转向方法 二.HyperLink控件   1. Asp.net 服务器端控件 属性 ...

  3. css透明属性

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  4. Java中可变长参数的使用及注意事项

    在Java5 中提供了变长参数(varargs),也就是在方法定义中可以使用个数不确定的参数,对于同一方法可以使用不同个数的参数调用,例如print("hello");print( ...

  5. C# 清空sessin

    Session.Abandon();//清除全部Session//清除某个SessionSession["UserName"] = null;Session.Remove(&quo ...

  6. hdu2067

    如果i==j&&j-1>=0时候,f[i][j]=f[i][j-1]; 如果j==0时候,f[i][j]=1; 其他 f[i][j]=f[i-1][j]+f[i][j-1]; # ...

  7. Sublime Text 2/3安装CTags实现函数跳转

    安装ctags 下载 ctags程序,放到目录D:/ctags/下 安装ctags插件 1. 打开Sublime Text 2. Preferences->Package Control-> ...

  8. 基本的Web控件三

    基本的Web控件用法一 ListBox控件 页面布局: <div> <h1>ListBox控件</h1> 学生列表: <br/> <asp:Lis ...

  9. 基本的Web控件一

    ASP.NET提供了与HTML元素相对应的基本Web控件,ASP.NET提供的基本的Web控件如下: 基本的Web控件       对应的HTML元素 Label  ----------------- ...

  10. 关于使用SVN update时出现:E155004错误

    今天早上到公司开了电脑,准备update下SVN的代码,但是在update时出现svn: E155004: Working copy 'E:\XX' locked  错误,乍眼一看以为是什么配置出错, ...