我正在尝试从Python中的hdf5文件读取数据。 我可以使用h5py读取hdf5文件,但无法弄清楚如何访问文件中的数据。
我的密码
import h5py
import numpy as np
f1 = h5py.File(file_name,'r+')
这可以正常工作并读取文件。 但是,如何访问文件对象f1中的数据?
如果文件包含Keras模型,则您可能需要用Keras加载它。
读取HDF5
import h5py
filename = 'file.hdf5'
with h5py.File(filename, 'r') as f:
# List all groups
print("Keys: %s" % f.keys())
a_group_key = list(f.keys())[0]
# Get the data
data = list(f[a_group_key])
写HDF5
#!/usr/bin/env python
import h5py
# Create random data
import numpy as np
data_matrix = np.random.uniform(-1, 1, size=(10, 3))
# Write data to HDF5
with h5py.File('file.hdf5', 'w') as data_file:
data_file.create_dataset('group_name', data