我正试图在Python中从hdf5文件中读取数据。使用h5py
,我可以读取hdf5文件,但我无法弄清如何访问文件中的数据。
我的代码
import h5py
import numpy as np
f1 = h5py.File(file_name,'r+')
这样可以工作,文件也已经被读取。但是我该如何访问文件对象f1
中的数据?
利用这个问题的一些答案和最新的doc,我能够提取我的数值数组,使用以下方法:
import h5py
with h5py.File(filename, 'r') as h5f:
h5x = h5f[list(h5f.keys())[0]]['x'][()]
其中'x'
在我的情况下只是X坐标。
我推荐使用h5py的包装器H5Attr
,它允许您通过诸如group.dataset
(相当于原始的group['dataset']
)等属性轻松加载hdf5数据,并使用IPython/Jupyter选项卡完成。
代码在这里。 以下是一些使用示例,您可以自己尝试下面的代码
# create example HDF5 file for this guide
import h5py, io
file = io.BytesIO()
with h5py.File(file, 'w') as fp:
fp['0'] = [1, 2]
fp['a'] = [3, 4]
fp['b/c'] = 5
fp.attrs['d'] = 's'
# import package
from h5attr import H5Attr
# open file
f = H5Attr(file)
# easy access to members, with tab completion in IPython/Jupyter
f.a, f['a']
# also work for subgroups, but note that f['b/c'] is more efficient
# because it does not create f['b']
f.b.c, f['b'].c, f['b/c']
# access to HDF5 attrs via a H5Attr wrapper
f._attrs.d, f._attrs['d']
# show summary of the data
f._show()
# 0 int64 (2,)
# a int64 (2,)
# b/ 1 members
# lazy (default) and non-lazy mode
f = H5Attr(file)
f.a # <HDF5 dataset "a": shape (2,), type "<i8">
f = H5Attr(file, lazy=False)
f.a # array([3, 4])
使用visititems
函数来自h5py
。回调函数通过所有的层次结构:组和数据集进行调用。
import h5py
# Open the HDF5 file in read mode
file_path = 'your_file.h5'
with h5py.File(file_path, 'r') as file:
# Function to recursively print the HDF5 dataset hierarchy
def print_hdf5_item(name, obj):
# name is in path format like /group1/group2/dataset
if isinstance(obj, h5py.Group):
# Do something like creating a dictionary entry
print(f'Group: {name}')
elif isinstance(obj, h5py.Dataset):
# Do something with obj like converting to a pandas.Series
# and storing to a dictionary entry
print(f'Dataset: {name}')
# Visit all items in the HDF5 file and print their names
file.visititems(print_hdf5_item)
pandas.read_hdf
函数:import pandas as pd
df = pd.read_hdf('./store.h5')
如果使用Pandas,你可以使用pandas.DataFrame.to_hdf
:
# df is a DataFrame object
df.to_hdf('database.h5', 'group/subgroup', table=True, mode='a')
hdf5
文件和一个hdf
文件有什么不同?我有一些hdf
文件(它们是几个图像波段),但我无法弄清如何打开它们。 - mikey