小智 0
假设您的二进制平面文件在文件中的某个偏移量(以字节为单位)处包含 10000 个 float32,并且您希望将它们读回形状为 (10, 20, 50) 的 3D 数组数据,下面的代码应该可以完成这项工作
input_file = "filename.raw"
output_file = "filename.h5"
offset = OFFSET
shape = (10, 20, 50)
size = 4 * shape[0] * shape[1] * shape[2] # 4 bytes per element
import h5py
import numpy
h5 = h5py.File(output_file, "w")
dataset = h5["/"].create_dataset("data",
shape=shape,
dtype=numpy.float32,
external=((input_file, offset, size),))
h5.flush()
h5.close()
Run Code Online (Sandbox Code Playgroud)
对于创建的数据集应使用不同文件或偏移量的部分构建的情况,关键字external需要多种形式的元组(文件名、偏移量、大小)。
| 归档时间: |
|
| 查看次数: |
508 次 |
| 最近记录: |