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

转载自:http://www.cnblogs.com/gala/archive/2011/09/22/2184801.html

这篇文章写的很好,所以无耻的转了。。

有的时候需要用python处理二进制数据,比如,存取文件,socket操作时.这时候,可以使用python的struct模块来完成.可以用 struct来处理c语言中的结构体.

struct模块中最重要的三个函数是pack(), unpack(), calcsize()

?
# 四号程序员 http://www.coder4.com
1
2
3
4
5
6
7
8
#  按照给定的格式(fmt),把数据封装成字符串(实际上是类似于c结构体的字节流)
pack(fmt, v1, v2, ...)
 
# 按照给定的格式(fmt)解析字节流string,返回解析出来的tuple
unpack(fmt, string)      
 
# 计算给定的格式(fmt)占用多少字节的内存
calcsize(fmt)

上述fmt中,支持的格式为:

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    
p char[] string    
P void * integer   (5), (3)

注1.q和Q只在机器支持64位操作时有意思

注2.每个格式前可以有一个数字,表示个数

注3.s格式表示一定长度的字符串,4s表示长度为4的字符串,但是p表示的是pascal字符串

注4.P用来转换一个指针,其长度和机器字长相关

注5.最后一个可以用来表示指针类型的,占4个字节

为了同c中的结构体交换数据,还要考虑有的c或c++编译器使用了字节对齐,通常是以4个字节为单位的32位系统,故而struct根据本地机器字节顺序转换.可以用格式中的第一个字符来改变对齐方式.定义如下:

CHARACTER BYTE ORDER SIZE ALIGNMENT
@ native native native
= native standard none
< little-endian standard none
> big-endian standard none
! network (= big-endian) standard none

使用方法是放在fmt的第一个位置,就像’@5s6sif’

例子1:

结构体如下:

?
# 四号程序员 http://www.coder4.com
1
2
3
4
5
6
7
struct Header
{
     unsigned short id;
     char [4] tag;
     unsigned int version;
     unsigned int count;
}

通过socket.recv接收到了一个上面的结构体数据,存在字符串s中,现在需要把它解析出来,可以使用unpack()函数:

?
# 四号程序员 http://www.coder4.com
1
2
import struct
id , tag, version, count = struct.unpack( "!H4s2I" , s)

上面的格式字符串中,!表示我们要使用网络字节顺序解析,因为我们的数据是从网络中接收到的,在网络上传送的时候它是网络字节顺序的.后面的H表示 一个unsigned short的id,4s表示4字节长的字符串,2I表示有两个unsigned int类型的数据.

就通过一个unpack,现在id, tag, version, count里已经保存好我们的信息了.

同样,也可以很方便的把本地数据再pack成struct格式:

?
# 四号程序员 http://www.coder4.com
1
ss = struct.pack( "!H4s2I" , id , tag, version, count);

pack函数就把id, tag, version, count按照指定的格式转换成了结构体Header,ss现在是一个字符串(实际上是类似于c结构体的字节流),可以通过 socket.send(ss)把这个字符串发送出去。

例子2:

?
# 四号程序员 http://www.coder4.com
1
2
3
4
5
6
import struct
 
a = 12.34
 
# 将a变为二进制
bytes = struct.pack( 'i' ,a)

此时bytes就是一个string字符串,字符串按字节同a的二进制存储内容相同。

再进行反操作,现有二进制数据bytes,(其实就是字符串),将它反过来转换成python的数据类型:

?
# 四号程序员 http://www.coder4.com
1
2
# 注意,unpack返回的是tuple !!
a, = struct.unpack( 'i' ,bytes)

如果是由多个数据构成的,可以这样:

?
# 四号程序员 http://www.coder4.com
1
2
3
4
5
6
a = 'hello'
b = 'world!'
c = 2
d = 45.123
 
bytes = struct.pack( '5s6sif' ,a,b,c,d)

此时的bytes就是二进制形式的数据了,可以直接写入文件比如 binfile.write(bytes)

然后,当我们需要时可以再读出来,bytes=binfile.read()

再通过struct.unpack()解码成python变量:

?
# 四号程序员 http://www.coder4.com
1
a,b,c,d = struct.unpack( '5s6sif' ,bytes)

’5s6sif’这个叫做fmt,就是格式化字符串,由数字加字符构成,5s表示占5个字符的字符串,2i,表示2个整数等等,下面是可用的字符及类型,ctype表示可以与python中的类型一一对应。

注意:二进制文件处理时会碰到的问题

我们使用处理二进制文件时,需要用如下方法:

?
# 四号程序员 http://www.coder4.com
1
2
3
binfile = open (filepath, 'rb' )     #读二进制文件
 
binfile = open (filepath, 'wb' )    #写二进制文件

那么和binfile=open(filepath,’r')的结果到底有何不同呢?

不同之处有两个地方:

第一,使用’r'的时候如果碰到’0x1A’,就会视为文件结束,这就是EOF。使用’rb’则不存在这个问题。即,如果你用二进制写入再用文本读出的话,如果其中存在’0X1A’,就只会读出文件的一部分。使用’rb’的时候会一直读到文件末尾。

第二,对于字符串x=’abc\ndef’,我们可用len(x)得到它的长度为7,\n我们称之为换行符,实际上是’0X0A’。当我们用’w'即文本方式写的时候,在windows平台上会自动将’0X0A’变成两个字符’0X0D’,’0X0A’,即文件长度实际上变成8.。当用’r'文本方式读取时,又自动的转换成原来的换行符。如果换成’wb’二进制方式来写的话,则会保持一个字符不变,读取时也是原样读取。所以如果用文本方式写入,用二进制方式读取的话,就要考虑这多出的一个字节了。’0X0D’又称回车符。linux下不会变。因为linux只使用’0X0A’来表示换行。



mport struct

pack、unpack、pack_into、unpack_from

[python]  view plain copy print ?
  1. # ref: http://blog.csdn.net/JGood/archive/2009/06/22/4290158.aspx  
  2.   
  3. import struct  
  4.   
  5. #pack - unpack  
  6. print  
  7. print '===== pack - unpack ====='  
  8.   
  9. str = struct.pack("ii"20400)  
  10. print 'str:', str  
  11. print 'len(str):', len(str) # len(str): 8   
  12.   
  13. a1, a2 = struct.unpack("ii", str)  
  14. print "a1:", a1  # a1: 20  
  15. print "a2:", a2  # a2: 400  
  16.   
  17. print 'struct.calcsize:', struct.calcsize("ii"# struct.calcsize: 8  
  18.   
  19.   
  20. #unpack  
  21. print  
  22. print '===== unpack ====='  
  23.   
  24. string = 'test astring'  
  25. format = '5s 4x 3s'  
  26. print struct.unpack(format, string) # ('test ', 'ing')  
  27.   
  28. string = 'he is not very happy'  
  29. format = '2s 1x 2s 5x 4s 1x 5s'  
  30. print struct.unpack(format, string) # ('he', 'is', 'very', 'happy')  
  31.   
  32.   
  33. #pack  
  34. print  
  35. print '===== pack ====='  
  36.   
  37. a = 20  
  38. b = 400  
  39.   
  40. str = struct.pack("ii", a, b)  
  41. print 'length:', len(str) #length: 8  
  42. print str  
  43. print repr(str) # '/x14/x00/x00/x00/x90/x01/x00/x00'  
  44.   
  45.   
  46. #pack_into - unpack_from  
  47. print  
  48. print '===== pack_into - unpack_from ====='  
  49. from ctypes import create_string_buffer  
  50.   
  51. buf = create_string_buffer(12)  
  52. print repr(buf.raw)  
  53.   
  54. struct.pack_into("iii", buf, 012, -1)  
  55. print repr(buf.raw)  
  56.   
  57. print struct.unpack_from("iii", buf, 0)  
  58.                                                      

运行结果:

[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值。下面的例子将两个整数转换为字符串(字节流):

[python]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. #!/usr/bin/env python  
  2. #encoding: utf8  
  3.   
  4. import sys  
  5. reload(sys)  
  6. sys.setdefaultencoding("utf-8")  
  7.   
  8. import struct  
  9.   
  10. a = 20  
  11. b = 400   
  12. str = struct.pack("ii", a, b)  
  13. print 'length: ', len(str)          # length:  8  
  14. print str                           # 乱码:   
  15. 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),该函数返回一个元组。 

下面是一个简单的例子:

[python]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. #!/usr/bin/env python  
  2. #encoding: utf8  
  3.   
  4. import sys  
  5. reload(sys)  
  6. sys.setdefaultencoding("utf-8")  
  7.   
  8. import struct  
  9.   
  10. a = 20  
  11. b = 400   
  12.   
  13. # pack  
  14. str = struct.pack("ii", a, b)  
  15. print 'length: ', len(str)          # length:  8  
  16. print str                           # 乱码:   
  17. print repr(str)                     # '\x14\x00\x00\x00\x90\x01\x00\x00'  
  18.   
  19. # unpack  
  20. str2 = struct.unpack("ii", str)  
  21. print 'length: ', len(str2)          # length:  2  
  22. print str2                           # (20, 400)  
  23. print repr(str2)                     # (20, 400)  

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

[python]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  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了很久,才找到一个例子,贴出来共享一下:

[python]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. #!/usr/bin/env python  
  2. #encoding: utf8  
  3.   
  4. import sys  
  5. reload(sys)  
  6. sys.setdefaultencoding("utf-8")  
  7.   
  8. import struct  
  9. from ctypes import create_string_buffer  
  10.   
  11. buf = create_string_buffer(12)  
  12. print repr(buf.raw)     # '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'  
  13.   
  14. struct.pack_into("iii", buf, 012, -1)  
  15. print repr(buf.raw)     # '\x01\x00\x00\x00\x02\x00\x00\x00\xff\xff\xff\xff'  
  16.   
  17. print struct.unpack_from("iii", buf, 0)     # (1, 2, -1)  

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

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


struct 类型表

FormatC TypePython typeStandard sizeNotes
xpad byteno value  
ccharstring of length 11 
bsigned charinteger1(3)
Bunsigned charinteger1(3)
?_Boolbool1(1)
hshortinteger2(3)
Hunsigned shortinteger2(3)
iintinteger4(3)
Iunsigned intinteger4(3)
llonginteger4(3)
Lunsigned longinteger4(3)
qlong longinteger8(2), (3)
Qunsigned long longinteger8(2), (3)
ffloatfloat4(4)
ddoublefloat8(4)
schar[]string1 
pchar[]string  
Pvoid *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 C long 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__() raisesTypeError, 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, andDeprecationWarning 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-1 bytes 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 integer 0. 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.


  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值