numpy.lib.npyio.NpzFile#
- class numpy.lib.npyio.NpzFile(fid)[source]#
種類似字典的物件,在建構時提供的 ZIP 壓縮檔中檔案會延遲載入。
NpzFile
用於載入 NumPy.npz
資料封存格式的檔案。它假設封存檔中的檔案具有.npy
副檔名,其他檔案將被忽略。陣列和檔案字串會在透過
obj['key']
進行 getitem 存取或透過obj.f.key
進行屬性查找時延遲載入。所有檔案的列表(不含.npy
副檔名)可以使用obj.files
取得,而 ZipFile 物件本身可以使用obj.zip
取得。- 參數:
- fid檔案、字串或 pathlib.Path
要開啟的 ZIP 壓縮檔。這可以是檔案類物件或包含封存檔路徑的字串。
- own_fidbool,選用
NpzFile 是否應關閉檔案控制代碼。要求
fid
是檔案類物件。
範例
>>> import numpy as np >>> from tempfile import TemporaryFile >>> outfile = TemporaryFile() >>> x = np.arange(10) >>> y = np.sin(x) >>> np.savez(outfile, x=x, y=y) >>> _ = outfile.seek(0)
>>> npz = np.load(outfile) >>> isinstance(npz, np.lib.npyio.NpzFile) True >>> npz NpzFile 'object' with keys: x, y >>> sorted(npz.files) ['x', 'y'] >>> npz['x'] # getitem access array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) >>> npz.f.x # attribute lookup array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
- 屬性:
- files字串列表
封存檔中所有具有
.npy
副檔名的檔案列表。- zipZipFile 實例
使用 ZIP 壓縮檔初始化的 ZipFile 物件。
- fBagObj 實例
可以對其執行屬性操作的物件,作為對
NpzFile
實例本身進行 getitem 存取的替代方案。- allow_picklebool,選用
允許載入 pickle 資料。預設值:False
- pickle_kwargsdict,選用
要傳遞給 pickle.load 的額外關鍵字引數。這些僅在使用 Python 3 時載入在 Python 2 上儲存的物件陣列時才有用。
- max_header_sizeint,選用
標頭允許的最大大小。大型標頭可能無法安全地載入,因此需要明確傳遞更大的值。詳情請參閱
ast.literal_eval
。當傳遞 allow_pickle 時,此選項會被忽略。在這種情況下,根據定義,檔案是受信任的,並且限制是不必要的。
方法