python-3.x - 如何使用 h5py 从 hdf5 存储文件路径和读取文件路径?
问题描述
我想用 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)
推荐阅读
- javascript - 分配对象不能通过 Rest/Spread 运算符工作
- python - Scapy 没有扫描我网络中的所有设备
- python - 在特定列 >2 个文件中查找公共元素并打印每个文件中的相应行
- flutter - 如何在 Flutter 中的另一个小部件下方显示受约束的小部件?
- python - 从给定的范围数组生成一个跨维二维数组
- node.js - 从私有 gitlab 注册表安装多个 npm 包
- python - 更新 python 对象的多个属性 - 单元测试
- ubuntu - 如何在 OpenSSL 包中修复 Ubuntu 服务器上的 AttributeError?
- javascript - 电子邮件地址验证器
- scikit-learn - 在尝试在 Jupiter notebook 和 PYCHARM 中导入 sklearn 模块时,我不断收到以下错误