Python 在 H5PY 中打开文件时出错(未找到文件签名)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38089950/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Error opening file in H5PY (File signature not found)
提问by Anisha Singh
I've been using the following bit of code to open some HDF5 files, produced in MATLAB, in python using H5PY:
我一直在使用以下代码在 python 中使用 H5PY 打开一些在 MATLAB 中生成的 HDF5 文件:
import h5py as h5
data='dataset.mat'
f=h5.File(data, 'r')
However I'm getting the following error:
但是我收到以下错误:
OSError: Unable to open file (File signature not found)
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 : )
我已经检查过我尝试打开的文件是 7.3 版 MAT 文件并且是 HDF5 格式。事实上,我之前已经使用 H5PY 成功打开了相同的文件。我已经确认这些文件存在并且可以访问,所以我不确定错误来自哪里。任何建议将不胜感激,提前致谢:)
回答by Honeybear
Usually the message File signature not found
indicates either:
通常该消息File signature not found
表明:
1. Your file is corrupted.
1.您的文件已损坏。
... 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).
……我觉得最有可能。你说你以前打开过这些文件。也许您忘记关闭可能损坏文件的文件句柄。尝试使用 HDF5 实用程序检查文件h5debug
(如果您在操作系统上安装了 hdf5 库,则可以在命令行上使用,dpkg -s libhdf5-dev
在 Linux 上检查)。
2. The file is not in HDF5 format.
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:
这是您的错误消息的已知原因。但是既然你说你确定了,就是这种情况并且你以前打开过文件,我提供这个仅供参考,供其他可能在这里绊倒的人参考:
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:
自 2015 年 12 月(从 7.3 版开始),Matlab 文件在其MAT 文件级别 5 容器中使用基于 HDF5 的格式(更多文档)。早期版本的 MAT 文件(v4(1.0 级)、v6 和 v7 到 7.2)由scipy
库支持并可使用库读取:
import scipy.io
f = scipy.io.loadmat('dataset.mat')
Otherwise you may try other methods and see whether the error persists:
否则您可以尝试其他方法并查看错误是否仍然存在:
PyTablesis an alternative to h5py and be found here.
PyTables是h5py的替代品,可在此处找到。
import tables
file = tables.open_file('test.mat')
Install using
安装使用
pip install tables
Python MATLAB Engineis an alternative to read MAT files, if you have matlab installed. Documentation is found here: MATLAB Engine API for Python.
如果您安装了 matlab,Python MATLAB 引擎是读取 MAT 文件的替代方法。文档位于此处:MATLAB Engine API for Python。
import matlab.engine
mat = matlab.engine.start_matlab()
f = mat.load("dataset.mat", nargout=1)
回答by fateh
I was facing the same issue with my .h5 file. And the problem was that I was not downloading the .h5 file correctly.
我的 .h5 文件也面临同样的问题。问题是我没有正确下载 .h5 文件。
I was doing filename.h5->right_click->save link as, which was not downloading the file correctly(or may be the file was getting corrupted). Instead of doing that I downloaded the file as : selected the checkbox with filename.h5 and clicked on download and after that my code worked.
我在做 filename.h5->right_click->save link as,它没有正确下载文件(或者可能是文件被损坏了)。我没有这样做,而是将文件下载为:选择带有 filename.h5 的复选框,然后单击下载,然后我的代码就可以工作了。
May be this help the one's who are doing the same mistake.
可能这有助于那些犯同样错误的人。
回答by Dharmendra Singh
Usually this happens when files are corrupted. I faced this problem and downloaded the file again and it resolves the issues.
通常这发生在文件损坏时。我遇到了这个问题并再次下载了文件,它解决了问题。