简体   繁体   中英

Inspect .h5 file in Python

I have an issue regarding an .h5 file, which I don't really know what it contains. Actually, I want to check what is inside the file using Python, but every time I open it with pandas:

df = pd.read_hdf('file.h5')

it returns an error:

ValueError: No dataset in HDF5 file.

Any idea how to successfully open and inspect it? Thank you.

Pandas is more of a library for CSV or other delimiter separated values.

So, use h5py .

>>> import h5py
>>> f = h5py.File('mytestfile.h5', 'r')

The File object is your starting point .h5py.File acts like a Python dictionary, thus we can check the keys,

>>> list(f.keys())
['mydataset']

Based on our observation, there is one data set, mydataset in the file. Let us examine the data set as a Dataset object

>>> dset = f['mydataset']

The object we obtained isn't an array, but an HDF5 dataset. Like NumPy arrays, datasets have both a shape and a data type:

>>> dset.shape
(100,)
>>> dset.dtype
dtype('int32')

They also support array-style slicing. This is how you read and write data from a dataset in the file:

>>> dset[...] = np.arange(100)
>>> dset[0]
0
>>> dset[10]
10
>>> dset[0:100:10]
array([ 0, 10, 20, 30, 40, 50, 60, 70, 80, 90])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM