1. 程式人生 > >python struct模塊

python struct模塊

color describe format) native same exp amp functions div

 1 import struct  # a module
 2 
 3 
 4 # Functions to convert between Python values and C structs.
 5 # Python bytes objects are used to hold the data representing the C struct
 6 # and also as format strings (explained below) to describe the layout of data in the C struct.
 7 # @: native order, size & alignment (default)
8 # =: native order, std. size & alignment 9 # <: little-endian, std. size & alignment 10 # >: big-endian, std. size & alignment 11 # !: same as > 12 13 # x: pad byte (no data); c:char; b:signed byte; B:unsigned byte; 14 # ?: _Bool (requires C99; if not available, char is used instead)
15 # h:short; H:unsigned short; i:int; I:unsigned int; 16 # l:long; L:unsigned long; f:float; d:double; e:half-float.Special cases (preceding decimal count indicates length): 17 # s:string (array of char); p: pascal string (with count byte).Special cases (only available in native format): 18 # n:ssize_t; N:size_t;
19 # P:an integer type that is wide enough to hold a pointer.Special case (not in native mode unless ‘long long‘ in platform C): 20 # q:long long; Q:unsigned long long 21 # class Struct(object) Struct(fmt) --> compiled struct object 22 # Return a new Struct object which writes and reads binary data according to the format string fmt 23 24 # iter_unpack(...) S.iter_unpack(buffer) -> iterator(v1, v2, ...) 25 # 26 # pack_into(...) S.pack_into(buffer, offset, v1, v2, ...) 27 # unpack_from(...) S.unpack_from(buffer, offset=0) -> (v1, v2, ...) 28 # 29 # pack(...) S.pack(v1, v2, ...) -> bytes 30 # unpack(...) S.unpack(buffer) -> (v1, v2, ...) 31 32 33 import struct 34 35 36 # 1、struct.pack 37 a = 20 38 b = 400 39 40 str_ = struct.pack(">ii", a, b) # 轉換後的str雖然是字符串類型,但相當於其他語言中的字節流(字節數組),可以在網絡上傳輸 41 print(length:, len(str_)) 42 print(type:, type(str_)) 43 print(str_) 44 print(repr(str_)) 45 46 # length: 8 47 # type: <class ‘bytes‘> 48 # b‘\x00\x00\x00\x14\x00\x00\x01\x90‘ 49 # b‘\x00\x00\x00\x14\x00\x00\x01\x90‘ 50 51 52 str_ = struct.pack("<ii", a, b) # 轉換後的str雖然是字符串類型,但相當於其他語言中的字節流(字節數組),可以在網絡上傳輸 53 print(length:, len(str_)) 54 print(type:, type(str_)) 55 print(str_) 56 print(repr(str_)) 57 58 # length: 8 59 # type: <class ‘bytes‘> 60 # b‘\x14\x00\x00\x00\x90\x01\x00\x00‘ 61 # b‘\x14\x00\x00\x00\x90\x01\x00\x00‘ 62 63 64 # 2、struct.unpack 65 a1, a2 = struct.unpack("<ii", str_) # 返回元祖 66 print(a1, a2) # 20,400 67 68 69 # 3、struct.calcsize用於計算格式字符串所對應的結果的長度 70 print(struct.calcsize(>ii)) # 8 71 72 73 # 4、struct.pack_into, struct.unpack_from 74 from ctypes import create_string_buffer 75 76 buf = create_string_buffer(12) 77 print(repr(buf.raw)) # b‘\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00‘ 78 79 struct.pack_into(">iii", buf, 0, 1, 2, -1) # 0是offset 80 print(repr(buf.raw)) # b‘\x01\x00\x00\x00\x02\x00\x00\x00\xff\xff\xff\xff‘ 81 82 print(struct.unpack_from(>iii, buf, 0)) # (1, 2, -1) 83 84 # 5、struct.iter_unpack 85 ret = struct.iter_unpack(>iii, buf) 86 print(ret) # <unpack_iterator object at 0x0000000001D02E18> 87 for r in ret: 88 print(r) # (1, 2, -1) 89 90 91 # 6、練習 92 print(struct.pack(ci, b*, 0x12131415)) # b‘*\x00\x00\x00\x15\x14\x13\x12‘ 93 print(struct.pack(ic, 0x12131415, b*)) # b‘\x15\x14\x13\x12*‘ 94 95 record = braymond \x32\x12\x08\x01\x08 96 print(struct.unpack(<10sHHb, record)) # (b‘raymond ‘, 4658, 264, 8)
字符字節順序大小對齊
@ 本機 本機 本機
= 本機 標準
< 小端 標準
> 大端 標準
! 網絡(等於大端) 標準
格式C類型Python類型標準大小提示
x 填充字節 沒有值
c char 長度為1的字節 1
b signed char integer 1 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
n ssize_t integer 4
N size_t integet 4
e 7 float 2 5
f float float 4 5
d double float 8 5
s char[] bytes
p char[] bytes
P void * integer 6

python struct模塊