scipy.io.
readsav#
- scipy.io.readsav(file_name, idict=None, python_dict=False, uncompressed_file_name=None, verbose=False)[source]#
讀取 IDL .sav 檔案。
- 參數:
- file_namestr
IDL 儲存檔案的名稱。
- idictdict, optional
用於插入 .sav 檔案變數的字典。
- python_dictbool, optional
預設情況下,物件回傳值不是 Python 字典,而是一個不區分大小寫的字典,可透過項目、屬性和呼叫存取變數。若要取得標準 Python 字典,請將此選項設定為 True。
- uncompressed_file_namestr, optional
此選項僅對使用 /compress 選項寫入的 .sav 檔案有效。如果指定了檔名,壓縮後的 .sav 檔案將解壓縮到此檔案。否則,readsav 將使用
tempfile
模組自動判斷暫存檔名,並在成功讀取後移除暫存檔。- verbosebool, optional
是否列印出有關儲存檔案的資訊,包括讀取的記錄和可用的變數。
- 回傳值:
- idl_dictAttrDict 或 dict
如果 python_dict 設定為 False (預設值),此函式會回傳一個不區分大小寫的字典,可透過項目、屬性和呼叫存取變數。如果 python_dict 設定為 True,此函式會回傳一個 Python 字典,其中所有變數名稱皆為小寫。如果指定了 idict,則變數會寫入指定的字典,並回傳更新後的字典。
範例
>>> from os.path import dirname, join as pjoin >>> import scipy.io as sio >>> from scipy.io import readsav
從 tests/data 目錄取得範例 .sav 檔案的檔名。
>>> data_dir = pjoin(dirname(sio.__file__), 'tests', 'data') >>> sav_fname = pjoin(data_dir, 'array_float32_1d.sav')
載入 .sav 檔案內容。
>>> sav_data = readsav(sav_fname)
取得 .sav 檔案內容的鍵。
>>> print(sav_data.keys()) dict_keys(['array1d'])
使用鍵存取內容。
>>> print(sav_data['array1d']) [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. 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. 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. 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. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]