Scipy 简明教程

SciPy - Input & Output

Scipy.io(输入输出)包提供了一系列函数,用于处理不同格式的文件。某些格式包括:

The Scipy.io (Input and Output) package provides a wide range of functions to work around with different format of files. Some of these formats are −

  1. Matlab

  2. IDL

  3. Matrix Market

  4. Wave

  5. Arff

  6. Netcdf, etc.

下面详细介绍一下最常用的文件格式:

Let us discuss in detail about the most commonly used file formats −

MATLAB

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

Following are the functions used to load and save a .mat file.

Sr. No.

Function & Description

1

loadmat Loads a MATLAB file

2

savemat Saves a MATLAB file

3

whosmat Lists variables inside a MATLAB file

让我们考虑以下示例。

Let us consider the following example.

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

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

The above program will generate the following output.

{
   '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__': []
}

我们可以看到阵列及元信息。如果我们希望检查一个 MATLAB 文件的内容而不将数据读入到内存中,则使用 whosmat command ,如下所示。

We can see the array along with the Meta information. If we want to inspect the contents of a MATLAB file without reading the data into memory, use the whosmat command as shown below.

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

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

The above program will generate the following output.

[('vect', (1, 10), 'int64')]