Writing

For writing, instantiate an mmnpz.NpzWriter.

class mmnpz.NpzWriter(fn, mode='x')[source]

Helper class to sequentially add arrays to a given uncompressed .npz file.

Parameters:
  • fn (str or Path) – The zipped archive to replace, create, or append to.

  • mode (str, optional) – The mode can be either write ‘w’, exclusive create ‘x’, or append ‘a’. It defaults to ‘x’, so it will fail for existing files. Use ‘w’ to replace existing files, or ‘a’ to append to existing files.

Examples

Store data sequentially on disk, possibly overwriting an existing .npz file:

>>> with mmnpz.NpzWriter("/tmp/123.npz", mode="w") as f:
>>>     f.write("a", [[1, 2], [3, 4]])
>>>     f.write("b", [5, 6, 7, 8])

Append more data later:

>>> with mmnpz.NpzWriter("/tmp/123.npz", mode="a") as f:
>>>     f.write("c", [[9.5], [10.5]])
zip

The ZipFile object for accessing the .npz file.

Type:

ZipFile instance

write(name, array)[source]

Writes the given array under the given name.

Parameters:
  • name (str) – File name in the archive to save as, without its .npy extension. Supports directories separated by slashes.

  • array (array-like) – Array to save to the file.

Return type:

None

close()[source]

Close the file.

Return type:

None