Save several arrays into a single file in uncompressed ``.npz`` format. 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 name
(file, *args, **kwds)
| 554 | |
| 555 | @array_function_dispatch(_savez_dispatcher) |
| 556 | def savez(file, *args, **kwds): |
| 557 | """Save several arrays into a single file in uncompressed ``.npz`` format. |
| 558 | |
| 559 | Provide arrays as keyword arguments to store them under the |
| 560 | corresponding name in the output file: ``savez(fn, x=x, y=y)``. |
| 561 | |
| 562 | If arrays are specified as positional arguments, i.e., ``savez(fn, |
| 563 | x, y)``, their names will be `arr_0`, `arr_1`, etc. |
| 564 | |
| 565 | Parameters |
| 566 | ---------- |
| 567 | file : str or file |
| 568 | Either the filename (string) or an open file (file-like object) |
| 569 | where the data will be saved. If file is a string or a Path, the |
| 570 | ``.npz`` extension will be appended to the filename if it is not |
| 571 | already there. |
| 572 | args : Arguments, optional |
| 573 | Arrays to save to the file. Please use keyword arguments (see |
| 574 | `kwds` below) to assign names to arrays. Arrays specified as |
| 575 | args will be named "arr_0", "arr_1", and so on. |
| 576 | kwds : Keyword arguments, optional |
| 577 | Arrays to save to the file. Each array will be saved to the |
| 578 | output file with its corresponding keyword name. |
| 579 | |
| 580 | Returns |
| 581 | ------- |
| 582 | None |
| 583 | |
| 584 | See Also |
| 585 | -------- |
| 586 | save : Save a single array to a binary file in NumPy format. |
| 587 | savetxt : Save an array to a file as plain text. |
| 588 | savez_compressed : Save several arrays into a compressed ``.npz`` archive |
| 589 | |
| 590 | Notes |
| 591 | ----- |
| 592 | The ``.npz`` file format is a zipped archive of files named after the |
| 593 | variables they contain. The archive is not compressed and each file |
| 594 | in the archive contains one variable in ``.npy`` format. For a |
| 595 | description of the ``.npy`` format, see :py:mod:`numpy.lib.format`. |
| 596 | |
| 597 | When opening the saved ``.npz`` file with `load` a `NpzFile` object is |
| 598 | returned. This is a dictionary-like object which can be queried for |
| 599 | its list of arrays (with the ``.files`` attribute), and for the arrays |
| 600 | themselves. |
| 601 | |
| 602 | Keys passed in `kwds` are used as filenames inside the ZIP archive. |
| 603 | Therefore, keys should be valid filenames; e.g., avoid keys that begin with |
| 604 | ``/`` or contain ``.``. |
| 605 | |
| 606 | When naming variables with keyword arguments, it is not possible to name a |
| 607 | variable ``file``, as this would cause the ``file`` argument to be defined |
| 608 | twice in the call to ``savez``. |
| 609 | |
| 610 | Examples |
| 611 | -------- |
| 612 | >>> from tempfile import TemporaryFile |
| 613 | >>> outfile = TemporaryFile() |