SciPy - 輸入和輸出



Scipy.io(輸入和輸出)軟體包提供了大量函式,可用於處理不同格式的檔案。其中一些格式為 −

  • Matlab
  • IDL
  • 矩陣市場
  • Arff
  • Netcdf 等

接下來讓我們詳細討論最常用的檔案格式 −

MATLAB

以下是用於載入和儲存 .mat 檔案的函式。

序號。 功能和說明
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__': []
}

我們可以看到元資訊和陣列。如果我們希望檢查 MATLAB 檔案的內容而不將資料讀入記憶體,則使用 whosmat 命令,如下所示。

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

上述程式將生成以下輸出。

[('vect', (1, 10), 'int64')]
廣告
© . All rights reserved.