How to read binary files with numpy?

In NumPy, we can use the numpy.fromfile() function to read binary files. This function reads the contents of the file into a NumPy array.

Here is a simple example demonstrating how to use the fromfile() function to read a binary file:

import numpy as np

# 从二进制文件中读取数据
data = np.fromfile('binary_file.bin', dtype=np.float32)

# 打印读取的数据
print(data)

In the example above, we assume there is a binary file named binary_file.bin containing floating-point data. We use np.float32 as the data type, so the data read will be stored in the data array as 32-bit floating-point numbers.

Please modify the file names and data types in the code according to the actual circumstances.

bannerAds