如何在关闭文件后保持h5py组在内存中?
我该如何在关闭文件后保持h5py组在内存中?
在运行以下代码之后:
import h5py
feature_file = h5py.File(worm_file_path, 'r')
worm_features = feature_file["worm"]
我可以访问 worm_features
,因为它是一个 h5py组(<HDF5 group "/worm" (4 members)>
)
但是在我执行这一行之后:
feature_file.close()
我就无法再访问 worm_features
了。它现在显示为 <Closed HDF5 group>
。
因为我需要加载大约20个文件中的worm_features h5py组,所以我想在处理我已经加载到内存中的数据之前关闭这些文件。这难道不可能吗?
2 个回答
0
使用 [:]
可以把数据集的值复制到一个变量中,这样在关闭 hdf5 文件后你仍然可以访问这个数据集。
import h5py
feature_file = h5py.File(worm_file_path, 'r')
worm_features = feature_file["worm"] [:]
feature_file.close()
print (worm_features)
.value
对我来说不管用,还报了以下错误:
AttributeError: 'Dataset' object has no attribute 'value'
4
使用 .value 来从数据集中提取你想要的变量。
举个例子:
import h5py
feature_file = h5py.File(worm_file_path, 'r')
worm_features = feature_file["worm"].value
feature_file.close()
print worm_features