简体   繁体   English

python 中的 HDF5:读取文件

[英]HDF5 in python: reading file

I have used this before with no problem, and it suddenly doesn't work:我以前用过这个没问题,突然就不行了:

import scipy.io
import numpy as np
import h5py
f = h5py.File('Dv25.mat','r')
D = f["Dv25"]

Output: Output:

Traceback (most recent call last):

  File "C:/Users/jonathan/Documents/Forskning/P1/Datab/Felles/Matart/Felles/datab.py", line 5, in 
<module>
    D = f["Dv25"]
  File "h5py\_objects.pyx", line 54, in h5py._objects.with_phil.wrapper
  File "h5py\_objects.pyx", line 55, in h5py._objects.with_phil.wrapper
  File "C:\Program Files\Python38\lib\site-packages\h5py\_hl\group.py", line 264, in __getitem__
    oid = h5o.open(self.id, self._e(name), lapl=self._lapl)
  File "h5py\_objects.pyx", line 54, in h5py._objects.with_phil.wrapper
  File "h5py\_objects.pyx", line 55, in h5py._objects.with_phil.wrapper
  File "h5py\h5o.pyx", line 190, in h5py.h5o.open
KeyError: "Unable to open object (object 'Dv25' doesn't exist)"

However, typing f gives:但是,键入 f 给出:

>>> f
<HDF5 file "Dv25.mat" (mode r)>

and exchanging Dv25.mat for Dv25 or using either ' or " doesn't help.并将 Dv25.mat 交换为 Dv25 或使用 ' 或 " 没有帮助。

What's the reason for this problem and how can I fix it?这个问题的原因是什么,我该如何解决? Clearly this file is "there"显然这个文件是“那里”

Thanks!!!谢谢!!!

Best, J最佳,J

Some h5py/HDF5 basics:一些 h5py/HDF5 基础知识:
f is a file object. f是一个文件 object。
f['Dv25'] is an object reference (either a group or dataset). f['Dv25']是 object 参考(组或数据集)。 In this case, it is a node named 'Dv25' at the root level ('/').在这种情况下,它是根级别 ('/') 上名为 'Dv25' 的节点。 Apparently an object by this name doesn't exist in your file.显然,您的文件中不存在此名称的 object。
Use f.keys() to get objects at the root level.使用f.keys()获取根级别的对象。 This will return a view of the root level objects;这将返回根级别对象的视图; they could be groups or data sets.它们可以是组或数据集。 You can iterate on them to get the names.您可以迭代它们以获取名称。 To print, use this loop: (that should identify the root level names):要打印,请使用此循环:(应标识根级别名称):

for i in f.keys():  
    print (i)

Note: .mat extensions usually come from Matlab, which has an option to write HDF5 formatted files.注意:.mat 扩展名通常来自 Matlab,它具有写入 HDF5 格式文件的选项。 Be sure this is written in that format.确保这是以该格式编写的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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