To use the iter_unpack function in Python to create a NumPy array, you can follow these steps:

  1. Import the necessary libraries:
import numpy as np
import struct
  1. Define the format string to unpack the binary data:
fmt = '<fff'

This format string specifies that the binary data contains three floats in little-endian byte order.

  1. Read the binary data into a buffer:
with open('data.bin', 'rb') as f:
    buf = f.read()
  1. Use iter_unpack to unpack the binary data into a list of tuples:
data = list(struct.iter_unpack(fmt, buf))
  1. Convert the list of tuples to a NumPy array:
arr = np.array(data)

The resulting arr array will have shape (N, 3), where N is the number of tuples in the binary data file. Each row of the array will contain the three floats from the corresponding tuple in the binary data.

python iter_unpack to numpy array

原文地址: https://www.cveoy.top/t/topic/brG5 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录