numpy.
savez
将多个数组保存到未压缩的单个文件中 .npz 格式。
.npz
提供数组作为关键字参数,以将它们存储在输出文件的相应名称下: savez(fn, x=x, y=y) .
savez(fn, x=x, y=y)
如果数组被指定为位置参数,即。, savez(fn, x, y) ,他们的名字是 arr_0 , arr_1 等。
savez(fn, x, y)
保存数据的文件名(字符串)或打开的文件(类似文件的对象)。如果文件或字符串是 .npz 如果文件名还没有扩展名,则扩展名将附加到文件名中。
要保存到文件的数组。请使用关键字参数(请参阅 kwds 下面)为数组指定名称。指定为args的数组将被命名为“arru 0”、“arru 1”,依此类推。
要保存到文件的数组。每个数组都将以其相应的关键字名称保存到输出文件中。
参见
save
将单个数组保存为numpy格式的二进制文件。
savetxt
将数组保存为纯文本文件。
savez_compressed
将多个数组保存到压缩的 .npz 档案文件
笔记
这个 .npz 文件格式是以文件包含的变量命名的压缩文件存档。存档未压缩,存档中的每个文件都包含一个变量 .npy 格式。关于 .npy 格式,参见 numpy.lib.format .
.npy
numpy.lib.format
打开保存的 .npz 用文件 load 一 NpzFile 返回对象。这是一个类似字典的对象,可以查询它的数组列表(使用 .files 属性),以及数组本身。
load
.files
保存词典时,词典键将成为ZIP存档中的文件名。因此,键应该是有效的文件名。E、 例如,避免使用以 / 或包含 . .
/
.
实例
>>> from tempfile import TemporaryFile >>> outfile = TemporaryFile() >>> x = np.arange(10) >>> y = np.sin(x)
使用 savez 具有 * 参数,数组以默认名称保存。
>>> np.savez(outfile, x, y) >>> _ = outfile.seek(0) # Only needed here to simulate closing & reopening file >>> npzfile = np.load(outfile) >>> npzfile.files ['arr_0', 'arr_1'] >>> npzfile['arr_0'] array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
使用 savez 具有 * *kwds,数组与关键字名称一起保存。
>>> outfile = TemporaryFile() >>> np.savez(outfile, x=x, y=y) >>> _ = outfile.seek(0) >>> npzfile = np.load(outfile) >>> sorted(npzfile.files) ['x', 'y'] >>> npzfile['x'] array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])