当前位置: 代码迷 >> python >> 如何在现有HDF5数据集中插入/编辑列
  详细解决方案

如何在现有HDF5数据集中插入/编辑列

热度:48   发布时间:2023-07-16 11:00:02.0

我有一个HDF5文件,如下所示。 我想编辑索引列并创建一个新的时间戳索引。 有什么办法吗?

除非您首先具有用于创建HDF5文件的方案/规范,否则这是不可能的。

如果您尝试使用HDF5文件(如电子表格)(甚至通过h5py),可能会出错。 例如:

  • 块形状,压缩,数据类型不一致。
  • 同类数据变得非同类。

您可以做的是将列表作为属性添加到数据集。 实际上,这可能是正确的事情。 下面的示例代码,输入为字典。 当您读入数据时,会将属性链接到同类数据(按行,列或其他标识符)。

def add_attributes(hdf_file, attributes, path='/'):

    """Add or change attributes in path provided.
    Default path is root group.
    """

    assert os.path.isfile(hdf_file), "File Not Found Exception '{0}'.".format(hdf_file)
    assert isinstance(attributes, dict), "attributes argument must be a key: value dictionary: {0}".format(type(attributes))

    with h5py.File(hdf_file, 'r+') as hdf:
        for k, v in attributes.items():
            hdf[path].attrs[k] = v

    return "The following attributes have been added or updated: {0}".format(list(attributes.keys()))