python 关于.mat文件读取的那些事

大家都知道.mat文件是matlab生成的,由于我对matlab很不熟悉所以,希望用python打开.mat文件。

python在读写matlab文件时常用到scipy.io文件;但,针对大文件或是存储版本在“matlab-v7.3”以上的文件就没有办法了。
所以这个时候我们就需要h5py来帮助我们。

对于h5py 如果我们直接cmd用pip进行下载

pip install h5py

 这个时候收到报错,或者没有成功安装的提示。

笔主尝试了镜像源网址http://pypi.doubanio.com/simple/,还是没有成功

转到https://pypi.org/project/h5py/#files,下载对应版本的文件

笔主是python3.6,win64


下载后,将后缀.whl 改成.zip,解压后放在/python/Lib 文件下

有些博客中说h5py需要在HDF5安装后才能运行,

http://pypi.doubanio.com/simple/中可以找到

成功后会发现


原因主要是 python2.x 和 python3.x对keys方法的返回处理不同。官方说明如下:

When using h5py from Python 3, the keys(), values() and items() methods will return view-like objects instead of lists. These objects support containership testing and iteration, but can’t be sliced like lists.

 python2 返回为list,而python3 返回为view-like objects,并不能直接查看。

解决方法:

1) 换成 python2.x 环境进行相同操作。 
2) 采用如下代码:

key = [key for key in mydata.keys()]
注意:如果是cell的,那么读取到的数据是原数据的转置,需要转置回来。对于一般的array结构的.mat不存在转置。
 
 

【参考】

https://blog.csdn.net/u013630349/article/details/47111773

猜你喜欢

转载自blog.csdn.net/weixin_42595525/article/details/80954459