python mat是什么文件

.mat就是matlab的文件格式,一般用于matlab和python间的数据传输,python中numpy和scipy提供了一些函数,可以很好的对.mat文件的数据进行读写和处理。

在python中可以使用scipy.io中的函数loadmat()读取mat文件,函数savemat保存文件。

1、读取文件

如上例:

复制代码
#coding:UTF-8
import scipy.io as scio
dataFile = 'E://data.mat'
data = scio.loadmat(dataFile)

注意,读取出来的data是字典格式,可以通过函数type(data)查看。

复制代码
print type(data)

结果显示:

复制代码
<type 'dict'>

找到mat文件中的矩阵:

复制代码
print data['A']

结果显示:

复制代码
[[ 0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0.
。。。。。。。。。。。
0. 0. 0. 0. 0. 0. 0.
0.36470588 0.90196078 0.99215686 0.99607843 0.99215686 0.99215686
0.78431373 0.0627451 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0.
。。。。。。。。。。。。
0.94117647 0.22745098 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0.30196078
。。。。。。。
0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0. ]]

格式为:

复制代码
<type 'numpy.ndarray'>

即为numpy中的矩阵格式。

相关推荐
明月_清风5 小时前
Python 内存手术刀:sys.getrefcount 与引用计数的生死时速
后端·python
明月_清风5 小时前
Python 消失的内存:为什么 list=[] 是新手最容易踩的“毒苹果”?
后端·python
Flittly20 小时前
【从零手写 ClaudeCode:learn-claude-code 项目实战笔记】(3)TodoWrite (待办写入)
python·agent
千寻girling1 天前
一份不可多得的 《 Django 》 零基础入门教程
后端·python·面试
databook1 天前
探索视觉的边界:用 Manim 重现有趣的知觉错觉
python·动效
明月_清风1 天前
Python 性能微观世界:列表推导式 vs for 循环
后端·python
明月_清风1 天前
Python 性能翻身仗:从 O(n) 到 O(1) 的工程实践
后端·python
helloweilei2 天前
python 抽象基类
python
用户8356290780512 天前
Python 实现 PPT 转 HTML
后端·python
zone77392 天前
004:RAG 入门-LangChain读取PDF
后端·python·面试