二進位制陣列

一枚码农發表於2024-04-27
點選檢視程式碼
# 讀寫二進位制結構陣列
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)]

相關文章