目录

SciPy - 输入和输出( Input and Output)

Scipy.io(输入和输出)包提供了广泛的功能,可以处理不同格式的文件。 其中一些格式是 -

  • Matlab
  • IDL
  • Matrix Market
  • Wave
  • Arff
  • Netcdf, etc.

让我们详细讨论最常用的文件格式 -

MATLAB

以下是用于加载和保存.mat文件的函数。

Sr. No. 功能说明
1

loadmat

加载MATLAB文件

2

savemat

保存MATLAB文件

3

whosmat

列出MATLAB文件中的变量

让我们考虑以下示例。

import scipy.io as sio
import numpy as np
#Save a mat file
vect = np.arange(10)
sio.savemat('array.mat', {'vect':vect})
#Now Load the File
mat_file_content = sio.loadmat(‘array.mat’)
Print mat_file_content

上述程序将生成以下输出。

{
   'vect': array([[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]]), '__version__': '1.0', 
   '__header__': 'MATLAB 5.0 MAT-file Platform: posix, Created on: Sat Sep 30 
   09:49:32 2017', '__globals__': []
}

我们可以看到数组以及Meta信息。 如果我们想要在不将数据读入内存的情况下检查MATLAB文件的内容,请使用whosmat command ,如下所示。

import scipy.io as sio
mat_file_content = sio.whosmat(‘array.mat’)
print mat_file_content

上述程序将生成以下输出。

[('vect', (1, 10), 'int64')]
↑回到顶部↑
WIKI教程 @2018