每週一個 Python 模塊 | struct

專欄地址:每週一個 Python 模塊html

struct 模塊包括用於在字節串和 Python 數據類型(如數字和字符串)之間進行轉換的函數。python

包裝和拆包

Structs 支持將數據打包成字符串,並使用格式說明符從字符串中解壓縮數據,格式說明符由表示數據類型的字符、可選計數和字節順序指示符組成。有關支持的格式說明符的完整列表,請參閱標準庫文檔。git

在此示例中,說明符調用整數或長整數值,雙字節字符串和浮點數。格式說明符中的空格用做分隔類型指示符,並在編譯格式時忽略。github

import struct
import binascii

values = (1, 'ab'.encode('utf-8'), 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))

# output
# Original values: (1, b'ab', 2.7)
# Format string : b'I 2s f'
# Uses : 12 bytes
# Packed Value : b'0100000061620000cdcc2c40'
複製代碼

該示例使用函數 binascii.hexlify() 將打包值轉換爲十六進制字節序列以進行打印。函數

unpack()從打包中提取數據。性能

import struct
import binascii

packed_data = binascii.unhexlify(b'0100000061620000cdcc2c40')

s = struct.Struct('I 2s f')
unpacked_data = s.unpack(packed_data)
print('Unpacked Values:', unpacked_data)

# output
# Unpacked Values: (1, b'ab', 2.700000047683716)
複製代碼

將打包值傳遞給 unpack(),返回基本相同的值(注意浮點值的差別)。優化

字節順序

默認狀況下,使用本機 C 庫的字節序對值進行編碼 。經過在格式字符串中提供顯式字節序指令,能夠輕鬆覆蓋該選項。編碼

import struct
import binascii

values = (1, 'ab'.encode('utf-8'), 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))
    
# output
# Original values: (1, b'ab', 2.7)
# 
# Format string : b'@ I 2s f' for native, native
# Uses : 12 bytes
# Packed Value : b'0100000061620000cdcc2c40'
# Unpacked Value : (1, b'ab', 2.700000047683716)
# 
# Format string : b'= I 2s f' for native, standard
# Uses : 10 bytes
# Packed Value : b'010000006162cdcc2c40'
# Unpacked Value : (1, b'ab', 2.700000047683716)
# 
# Format string : b'< I 2s f' for little-endian
# Uses : 10 bytes
# Packed Value : b'010000006162cdcc2c40'
# Unpacked Value : (1, b'ab', 2.700000047683716)
# 
# Format string : b'> I 2s f' for big-endian
# Uses : 10 bytes
# Packed Value : b'000000016162402ccccd'
# Unpacked Value : (1, b'ab', 2.700000047683716)
# 
# Format string : b'! I 2s f' for network
# Uses : 10 bytes
# Packed Value : b'000000016162402ccccd'
# Unpacked Value : (1, b'ab', 2.700000047683716)
複製代碼

緩衝區

使用二進制打包數據一般用於對性能要求較高的場景或將數據傳入和傳出擴展模塊。經過避免爲每一個打包結構分配新緩衝區,能夠優化這些狀況。pack_into()unpack_from()方法支持直接寫入預先分配的緩衝區。spa

import array
import binascii
import ctypes
import struct

s = struct.Struct('I 2s f')
values = (1, 'ab'.encode('utf-8'), 2.7)
print('Original:', values)

print()
print('ctypes string buffer')

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')

a = array.array('b', b'\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))

# output
# Original: (1, b'ab', 2.7)
# 
# ctypes string buffer
# Before : b'000000000000000000000000'
# After : b'0100000061620000cdcc2c40'
# Unpacked: (1, b'ab', 2.700000047683716)
# 
# array
# Before : b'000000000000000000000000'
# After : b'0100000061620000cdcc2c40'
# Unpacked: (1, b'ab', 2.700000047683716)
複製代碼

size屬性告訴咱們緩衝區須要多大。

相關文檔:code

pymotw.com/3/struct/in…

相關文章
相關標籤/搜索