numpy.lib.npyio.NpzFile#

classnumpy.lib.npyio.NpzFile(fid)[source]#

A dictionary-like object with lazy-loading of files in the zippedarchive provided on construction.

NpzFile is used to load files in the NumPy.npz data archiveformat. It assumes that files in the archive have a.npy extension,other files are ignored.

The arrays and file strings are lazily loaded on eithergetitem access usingobj['key'] or attribute lookup usingobj.f.key. A list of all files (without.npy extensions) canbe obtained withobj.files and the ZipFile object itself usingobj.zip.

Parameters:
fidfile, str, or pathlib.Path

The zipped archive to open. This is either a file-like objector a string containing the path to the archive.

own_fidbool, optional

Whether NpzFile should close the file handle.Requires thatfid is a file-like object.

Examples

>>>importnumpyasnp>>>fromtempfileimportTemporaryFile>>>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>>>npzNpzFile 'object' with keys: x, y>>>sorted(npz.files)['x', 'y']>>>npz['x']# getitem accessarray([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])>>>npz.f.x# attribute lookuparray([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
Attributes:
fileslist of str

List of all files in the archive with a.npy extension.

zipZipFile instance

The ZipFile object initialized with the zipped archive.

fBagObj instance

An object on which attribute can be performed as an alternativeto getitem access on theNpzFile instance itself.

allow_picklebool, optional

Allow loading pickled data. Default: False

pickle_kwargsdict, optional

Additional keyword arguments to pass on to pickle.load.These are only useful when loading object arrays saved onPython 2.

max_header_sizeint, optional

Maximum allowed size of the header. Large headers may not be safeto load securely and thus require explicitly passing a larger value.Seeast.literal_eval for details.This option is ignored whenallow_pickle is passed. In that casethe file is by definition trusted and the limit is unnecessary.

Methods

close()

Close the file.

get(key[, default])

D.get(k,[,d]) returns D[k] if k in D, else d.

items()

D.items() returns a set-like object providing a view on the items

keys()

D.keys() returns a set-like object providing a view on the keys

values()

D.values() returns a set-like object providing a view on the values

On this page