如何使用 h5py 存储文件路径和从 hdf5 读取文件路径?

How to store file paths and read file paths from hdf5 with h5py?

我想用 h5py 在我的 hdf5 中将一个很长的文件路径保存为字符串。我有以下代码,但它似乎不起作用。当我读取文件时,变量不显示文件路径。 请问怎样做比较好?谢谢。

import h5py

hdf5filename='testhdf5.h5'
hdf5dsetname_origin="/entry/origin/filename"

# create hdf5 file and store a very long file path

with h5py.File(hdf5filename, "w") as h5f:
    string_dt = h5py.special_dtype(vlen=str)
    h5f.create_dataset(hdf5dsetname_origin, data='/path/to/data/verylong/verylong/verlong/extralong',dtype=string_dt)           

# read it and check the file path

with h5py.File(hdf5filename,"r") as h5:
    string=h5["/entry/origin/filename"]

print(string)

在一些帮助下,我找到了解决方案。

hdf5filename='test2hdf5.h5'
hdf5dsetname_origin="entry/origin/filename"

datastring="/path/to/data/verylong/verylong/verlong/extralong"


with h5py.File(hdf5filename, "w") as h5f:
    string_dt = h5py.special_dtype(vlen=str)
    h5f.create_dataset(hdf5dsetname_origin, data=datastring, dtype=string_dt)

with h5py.File(hdf5filename,"r") as h5:
    print(h5.keys())
    string=h5["/entry/origin/filename"][()].decode('utf-8')
    print(string)

创建一个数据集来存储一小部分数据是多余的。属性就是为此目的而设计的(有时称为元数据)。下面的例子完成了这个任务。 (我用你的文件名创建了一个属性,再加上一些额外的属性来演示浮点数和整数的属性用法。)我将这些属性添加到根级别组中。您可以将属性添加到任何组或数据集。)在此处完成详细信息:h5py Attributes

hdf5filename='test2hdf5.h5'
hdf5dsetname_origin="entry/origin/filename"
datastring="/path/to/data/verylong/verylong/verlong/extralong"
    
with h5py.File(hdf5filename, "w") as h5w:
    h5w.attrs[hdf5dsetname_origin] = datastring
    h5w.attrs['version'] = 1.01
    h5w.attrs['int_attr3'] = 3

with h5py.File(hdf5filename,"r") as h5r:
    for attr in h5r.attrs.keys():
        print(f'{attr} => {h5r.attrs[attr]}')

输出如下所示:

entry/origin/filename => /path/to/data/verylong/verylong/verlong/extralong
int_attr3 => 3
version => 1.01