简体   繁体   中英

'ValuesViewHDF5' object has no attribute 'attrs'

f = h5py.File(data_dir+rec_filename+'.hdf5', 'r')
trial = f.values() #_._next_() #.next()
equilibrate = trial.attrs['equilibrate'] 

this synatax gives me error: 'ValuesViewHDF5' object has no attribute 'attrs'. Does anyone know why I get this error?

I tried to search if the syntax for the new h5py has changed but couldn't find anything relevant. Maybe it is a problem related to incpomatibilities between python2.x and python3.x

If you want to get the equilibrate attribute on the file object, you need to use this line:
equilibrate = f.attrs['equilibrate']

However as @hpaulj mentioned, you need to confirm the equilibrate attribute exists. You can access and print all attributes at the file level with this code:

with h5py.File(data_dir+rec_filename+'.hdf5', 'r') as f:   
    for k in f.attrs.keys():
        print(f"{k} => {f.attrs[k]}")

Complete details about creating and reading attributes with h5py are available in this answer to a similar question: How to read HDF5 attributes (metadata) with Python and h5py

BTW, why are you using f.values() ? It does NOT return the file object. If you inspect the returned object, you will find it has the objects from the referenced group (eg, Group and Dataset objects for f , the file object). Repeating what @hpaulj said, h5py uses dictionary syntax to access group and dataset names and/or objects (but they are not dictionaries.). The keys are the object names and the values are the objects: Here is a simple example showing how the dictionary syntax behaves:

with h5py.File(data_dir+rec_filename+'.hdf5', 'r') as f:   
    for k in f:  # get the keys, eg the object names
        print(f"Object name: {k}")
    for k in f.keys():  # same as example above
        print(f"Object name: {k}")
    for v in f.values(): # get the values, eg the objects
        print(f"Object: {v}; name: {v.name}")
    for k,v in f.items():  # get the keys and values
        print(f"Object name: {k}; Object: {v}")

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