二进制数组

点击查看代码
# 读写二进制结构数组
from functools import partial
from struct import Struct


# Write
def write_records(records, format, f):
    """Write a sequence of tuple to a binary file of structures"""
    record_struct = Struct(format)
    for r in records:
        f.write(record_struct.pack(*r))


# Write example
records = [(1, 2.3, 4.5), (6, 7.8, 9.0), (10, 12.13, 14.15)]
with open("binary_array", "wb") as f:
    write_records(records, "<idd", f)


# Read
def read_records(format, f):
    record_struct = Struct(format)
    # 利用 iter 和 partial 对固定大小的记录做迭代
    chunks = iter(partial(f.read, record_struct.size), b"")
    return (record_struct.unpack(chunk) for chunk in chunks)


# Read example
with open("binary_array", "rb") as f:
    for i in read_records("<idd", f):
        print("read data: ", i)


# 对于一个同大量二进制数据打交道的程序,最好使用像numpy这样的库来处理
import numpy as np

with open("binary_array", "rb") as f:
    records = np.fromfile(f, dtype="<i, <d, <d")
    print(
        "numpy read", records
    )  # [( 1,  2.3 ,  4.5 ) ( 6,  7.8 ,  9.  ) (10, 12.13, 14.15)]

posted @ 2024-04-27 14:25  一枚码农  阅读(5)  评论(0编辑  收藏  举报