Pabitra GHOSH
Pabitra GHOSH

Reputation: 11

I can not get keys information of hdf5 in python

import h5py
filename = '3DIMG_14MAY2016_0730_L1C_ASIA_MER.hdf5'
f = h5py.File("3DIMG_14MAY2016_0730_L1C_ASIA_MER.h5", 'r')
print("keys: %s" % f.keys())
KeysView(<HDF5 file "3DIMG_14MAY2016_0730_L1C_ASIA_MER.h5" (mode r)>)

Upvotes: 1

Views: 1388

Answers (1)

thebrandre
thebrandre

Reputation: 499

h5py provides access to hdf5 files as if the file (or a group inside the file) was a dictionary. With dictionaries, you encounter the same "problem" (it's actually a feature) that keys() does not return a list of elements but instead a generator:

dictExample = {"1": 1, "2": 2, "a" :0}
print(dictExample.keys())

The output is: dict_keys(['1', 'a', '2'])

You can convert it into a list and print it by:

print([x for x in dictExample.keys()])

In your specific example, you have to replace

print("keys: %s" % f.keys())

by

print([x for x in f.keys()])

This can be confusing because it's different from what you do in Python-2.7. You probably stumbled over some deprecated sample code.

Just a hint: if you are not sure whether the hd5 file contains the correct data, you may want to have a direct look at it using HDFView.

And btw: the filenames in your example do not match.

Upvotes: 2

Related Questions