在H5PY中打开文件时出错(找不到文件签名) [英] Error opening file in H5PY (File signature not found)

查看:986
本文介绍了在H5PY中打开文件时出错(找不到文件签名)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用以下代码使用H5PY在python中打开一些在MATLAB中生成的HDF5文件:

I've been using the following bit of code to open some HDF5 files, produced in MATLAB, in python using H5PY:

import h5py as h5
data='dataset.mat'
f=h5.File(data, 'r')

但是我遇到以下错误:

OSError: Unable to open file (File signature not found)

我已经检查过我要打开的文件是否为7.3版MAT文件且为HDF5格式.实际上,我之前已经使用H5PY成功打开了相同的文件.我已经确认文件存在并且可以访问,所以我不确定该错误来自何处.任何建议将不胜感激,在此先感谢:)

I've checked that the files that I'm trying to open are version 7.3 MAT-files and are HDF5 format. In fact I've used H5PY to open the same files successfully before. I've confirmed that the files exist and are accessible so I'm not really sure where the error is coming from. Any advice would be greatly appreciated, thanks in advance : )

推荐答案

通常消息File signature not found表示以下任一情况:

Usually the message File signature not found indicates either:

1.您的文件已损坏.

...这是我认为最有可能的情况.您说过您已经打开过文件.也许您忘记关闭可能损坏文件的文件句柄. 尝试使用HDF5实用程序h5debug检查文件(如果已在操作系统上安装了hdf5 lib,则可在命令行上找到,在Linux上使用dpkg -s libhdf5-dev检查).

... is what I think is most likely. You said you've opened the files before. Maybe you forgot closing your file-handle which can corrupt the file. Try checking the file with the HDF5 utility h5debug (available on command line if you've installed the hdf5 lib on your OS, check with dpkg -s libhdf5-dev on Linux).

2.该文件不是HDF5格式.

这是您的错误消息的已知原因.但是,既然您说了确定,就是这种情况,并且您之前已经打开过文件,所以我仅供参考,以供其他可能在这里绊倒的人参考:

This is a known cause for your error message. But since you said you made sure, that this is the case and you've opened the files before, I'm giving this just for reference for others that may stumble here:

自2015年12月(从7.3版开始),Matlab文件使用基于HDF5的格式,其 MAT文件第5级容器(更多 doc ).早期版本的MAT文件(v4(1.0版),v6和v7至7.2)受scipy库支持,并可以通过scipy库读取:

Since December 2015 (as of version 7.3), Matlab files use the HDF5 based format in their MAT-File Level 5 Containers (more doc). Earlier version MAT-files (v4 (Level 1.0), v6 and v7 to 7.2) are supported by and can be read with the scipy library:

import scipy.io
f = scipy.io.loadmat('dataset.mat')


否则,您可以尝试其他方法,看看错误是否仍然存在:


Otherwise you may try other methods and see whether the error persists:

PyTables 是h5py的替代方法,可在此处找到.

PyTables is an alternative to h5py and be found here.

import tables
file = tables.open_file('test.mat')

使用安装

pip install tables

如果安装了matlab,则

Python MATLAB Engine 是读取MAT文件的替代方法.可在此处找到文档:适用于Python的MATLAB引擎API

Python MATLAB Engine is an alternative to read MAT files, if you have matlab installed. Documentation is found here: MATLAB Engine API for Python.

import matlab.engine
mat = matlab.engine.start_matlab()
f = mat.load("dataset.mat", nargout=1)

这篇关于在H5PY中打开文件时出错(找不到文件签名)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆