- numpy.savez(file, *args, **kwds)¶
Save several arrays into a single file in uncompressed
Provide arrays as keyword arguments to store them under the corresponding name in the output file:
savez(fn, x=x, y=y).
If arrays are specified as positional arguments, i.e.,
savez(fn, x, y), their names will be arr_0, arr_1, etc.
- filestr or file
Either the filename (string) or an open file (file-like object) where the data will be saved. If file is a string or a Path, the
.npzextension will be appended to the filename if it is not already there.
- argsArguments, optional
Arrays to save to the file. Please use keyword arguments (see kwds below) to assign names to arrays. Arrays specified as args will be named “arr_0”, “arr_1”, and so on.
- kwdsKeyword arguments, optional
Arrays to save to the file. Each array will be saved to the output file with its corresponding keyword name.
.npzfile format is a zipped archive of files named after the variables they contain. The archive is not compressed and each file in the archive contains one variable in
.npyformat. For a description of the
When opening the saved
loada NpzFile object is returned. This is a dictionary-like object which can be queried for its list of arrays (with the
.filesattribute), and for the arrays themselves.
When saving dictionaries, the dictionary keys become filenames inside the ZIP archive. Therefore, keys should be valid filenames. E.g., avoid keys that begin with
>>> from tempfile import TemporaryFile >>> outfile = TemporaryFile() >>> x = np.arange(10) >>> y = np.sin(x)
savezwith *args, the arrays are saved with default names.
>>> 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])
savezwith **kwds, the arrays are saved with the keyword names.
>>> 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])