使用 h5py 在 HDF5 文件中自定义列名

Custom column names in HDF5 file using h5py

我有以下代码片段:

import h5py
import numpy

## Data set with shape (5, 5) and numpy array containing column names as string
data = numpy.random.random((5, 5))
column_names = numpy.array(["a", "b", "c", "d", "e"])

## Create file pointer
fp = h5py.File("data_set.HDF5", "w")

## Store data
fp["sub"] = data

## Close file
fp.close()

如何在 HDF5 file 中添加列的名称,如附图中的箭头所示?

诀窍是使用 Numpy dtype 来定义 field/column 名称,然后使用它来定义记录数组。您还可以混合变量类型(例如,如果您想在同一行中混合使用整数、浮点数和字符串)。

修改后的示例如下:

import h5py
import numpy as np

## Data set with shape (5, 5) and list containing column names as string
data = np.random.rand(5, 5)
col_names = ["a", "b", "c", "d", "e"]
## Create file pointer
with h5py.File("data_set_2.HDF5", "w") as fp :
    ds_dt = np.dtype( { 'names':col_names,
                        'formats':[ (float), (float), (float), (float), (float)] } )
    rec_arr = np.rec.array(data,dtype=ds_dt)        
    ## Store data
    ##fp["sub"] = data
    ds1 = fp.create_dataset('sub', data=rec_arr )