Python常用功能汇总

本文深入探讨了Python和Matlab之间的数据转换方法,包括如何使用scipy.io加载和写入.mat文件,以及使用h5py进行高级数据读取。文章详细介绍了数据读写流程、Python参数解析技巧,并提供了关于Python线程问题的理解实例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. Python数据和matlab数据转换

1.1 Python加载和写入.mat文件

File IO (scipy.io)

See also
numpy-reference.routines.io (in numpy)

MATLAB files

*loadmat(file_name[, mdict, appendmat])*    Load MATLAB file
*savemat(file_name, mdict[, appendmat, ...])*   Save a dictionary of names and arrays into a MATLAB-style .mat file.
*whosmat(file_name[, appendmat])*   List variables inside a MATLAB file

How to do

import scipy.io as sio
mat_contents = sio.loadmat('octave_a.mat')
mat_contents
>>>{'a': array([[[  1.,   4.,   7.,  10.],
        [  2.,   5.,   8.,  11.],
        [  3.,   6.,   9.,  12.]]]),
 '__version__': '1.0',
 '__header__': 'MATLAB 5.0 MAT-file, written by
 Octave 3.6.3, 2013-02-17 21:02:11 UTC',
 '__globals__': []}

oct_a = mat_contents['a']
oct_a
>>>array([[[  1.,   4.,   7.,  10.],
        [  2.,   5.,   8.,  11.],
        [  3.,   6.,   9.,  12.]]])

oct_a.shape
>>>(1, 3, 4)

sio.savemat('np_vector.mat', {'vect':vect})

If you want to inspect the contents of a MATLAB file without reading the data into memory, use the whosmat command:

sio.whosmat('octave_a.mat')
>>>[('a', (1, 3, 4), 'double')]

对于高版本的.mat数据读写,需要使用另外的接口

import h5py
datasets = F:/MuraDefectData_6X6_10W_NonZCA_Batches.mat'

f = h5py.File(datasets, 'r')
TotalBatchImg = f['TotalBatchImg']
TotalBatchImg = np.array(TotalBatchImg)
wSize = f['wSize']
wSize = np.array(wSize)
1.2 Python和matlab数据读写关系

—–未完待续

2.python参数解析argsparse

http://blog.xiayf.cn/2013/03/30/argparse/
http://blog.ixxoo.me/argparse.html
http://python.usyiyi.cn/python_278/library/argparse.html
http://yongli1992.com/?p=72

3.关于Python的线程问题

3个实例帮你理解Python中的线程
个人觉得讲的非常好~~赞一个

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值