[英]'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'.这个语法给我错误:“ValuesViewHDF5”object 没有属性“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.我试图搜索新 h5py 的语法是否已更改但找不到任何相关内容。 Maybe it is a problem related to incpomatibilities between python2.x and python3.x也许这是与 python2.x 和 python3.x 之间的 incpomatibilities 有关的问题
If you want to get the equilibrate
attribute on the file object, you need to use this line:如果你想获取文件 object 上的equilibrate
属性,你需要使用这一行:
equilibrate = f.attrs['equilibrate']
However as @hpaulj mentioned, you need to confirm the equilibrate
attribute exists.但是正如@hpaulj 提到的,您需要确认equilibrate
属性存在。 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有关使用 h5py 创建和读取属性的完整详细信息,请参阅类似问题的答案: How to read HDF5 attributes (metadata) with Python and h5py
BTW, why are you using f.values()
?顺便说一句,你为什么要使用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).它不返回文件 object。如果您检查返回的 object,您会发现它具有引用组中的对象(例如,文件对象f
的组和数据集对象)。 Repeating what @hpaulj said, h5py
uses dictionary syntax to access group and dataset names and/or objects (but they are not dictionaries.).重复@hpaulj 所说的, h5py
使用字典语法来访问组和数据集名称和/或对象(但它们不是字典。)。 The keys are the object names and the values are the objects: Here is a simple example showing how the dictionary syntax behaves:键是 object 名称,值是对象:这是一个简单的示例,显示字典语法的行为方式:
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}")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.