Python之array模块的使用
array模块的作用
目的:
有效管理固定类型数值数据的序列。
array模块定义了一个序列数据结构,看起来与list很相似,只不过所有成员都必须是相同的基本类型。
支持类型包括所有的数值类型或其他固定大小的基本类型(如字节)。
1、初始化
import array import binascii s = b'This is the array.' a = array.array('b', s) print('As byte string:', s) print('As array :', a) print('As hex :', binascii.hexlify(a))
运行效果
As byte string: b'This is the array.' As array : array('b', [84, 104, 105, 115, 32, 105, 115, 32, 116, 104, 101, 32, 97, 114, 114, 97, 121, 46]) As hex : b'54686973206973207468652061727261792e'
2、处理数组
import array a = array.array('i', range(3)) print('Initial :', a) a.extend(range(3)) print('Extended:', a) print('Slice :', a[2:5]) print('Iterator:') print(list(enumerate(a)))
运行效果
Initial : array('i', [0, 1, 2]) Extended: array('i', [0, 1, 2, 0, 1, 2]) Slice : array('i', [2, 0, 1]) Iterator: [(0, 0), (1, 1), (2, 2), (3, 0), (4, 1), (5, 2)]
3、将数组数据写入临时文件中
import array import binascii import tempfile a = array.array('i', range(5)) print('A1:', a) # 将数字数组写入临时文件 output = tempfile.NamedTemporaryFile() a.tofile(output.file) # must pass an *actual* file output.flush() # Read the raw data with open(output.name, 'rb') as input: raw_data = input.read() print('Raw Contents:', binascii.hexlify(raw_data)) # Read the data into an array input.seek(0) a2 = array.array('i') a2.fromfile(input, len(a)) print('A2:', a2)
运行效果
A1: array('i', [0, 1, 2, 3, 4]) Raw Contents: b'0000000001000000020000000300000004000000' A2: array('i', [0, 1, 2, 3, 4])
4、数组转字节,字节转数组的示例
import array import binascii a = array.array('i', range(5)) print('A1:', a) as_bytes = a.tobytes() print('Bytes:', binascii.hexlify(as_bytes)) a2 = array.array('i') a2.frombytes(as_bytes) print('A2:', a2)
运行效果
A1: array('i', [0, 1, 2, 3, 4]) Bytes: b'0000000001000000020000000300000004000000' A2: array('i', [0, 1, 2, 3, 4])
5、字节顺序的处理
import array import binascii def to_hex(a): chars_per_item = a.itemsize * 2 # 2 hex digits hex_version = binascii.hexlify(a) num_chunks = len(hex_version) // chars_per_item for i in range(num_chunks): start = i * chars_per_item end = start + chars_per_item yield hex_version[start:end] start = int('0x12345678', 16) end = start + 5 a1 = array.array('i', range(start, end)) a2 = array.array('i', range(start, end)) a2.byteswap() # 会用C交换数组中元素的字节顺序,这比用Python循环处理数据高效得多。 fmt = '{:>12} {:>12} {:>12} {:>12}' print(fmt.format('A1 hex', 'A1', 'A2 hex', 'A2')) print(fmt.format('-' * 12, '-' * 12, '-' * 12, '-' * 12)) fmt = '{!r:>12} {:12} {!r:>12} {:12}' for values in zip(to_hex(a1), a1, to_hex(a2), a2): print(fmt.format(*values))
运行效果
A1 hex A1 A2 hex A2 ------------ ------------ ------------ ------------ b'78563412' 305419896 b'12345678' 2018915346 b'79563412' 305419897 b'12345679' 2035692562 b'7a563412' 305419898 b'1234567a' 2052469778 b'7b563412' 305419899 b'1234567b' 2069246994 b'7c563412' 305419900 b'1234567c' 2086024210