'How to append an array to an existing `.npz` file?

I have the following code, which generate the mat file (in .npz format):

import numpy as np
x = np.arange(10)
np.savez('mat',x)

Now I want to append another array to the existing file:

y = np.arange(21)
np.savez('mat',y) 

But, it replaces the x array with y. I want to have both of these arrays in the file. Besides, I do not want to write them to the file at the same time.



Solution 1:[1]

Step1: create your first file

d = np.arange(10)
np.savez("mat",x = d)  # do not forget to name your variable d

Step2: read your file, convert it to a dictionary object, add your new variable to the dictionary, and finally overwrite your mat.npz with this dictionary

data = np.load("mat.npz")
data = dict(data)
data["y"] = np.arange(21)
np.savez("mat",**data)

Step 3: to verify if the last variable is added to the mat.npz file.

data = np.load("mat.npz")
print(data[x])   # [0 1 2 3 4 5 6 7 8 9]
print(data[y])   # [ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20]

Solution 2:[2]

I took the comments on the post as a start and the following code seems to do what you want. (I'm using python 3.6 and base modules only, it probably works for other python versions)

import zipfile
import io
import os
import numpy as np

test_np1 = np.random.random(100)
test_np2 = np.random.random(100)

# initial file
filename = 'npfile.npz'
try:
    np.savez_compressed(filename, test_np1=test_np1)

    with np.load(filename) as loaded_npz:
        print(np.array_equal(test_np1, loaded_npz['test_np1']))

    bio = io.BytesIO()
    np.save(bio, test_np2)
    with zipfile.ZipFile(filename, 'a') as zipf:
        # careful, the file below must be .npy
        zipf.writestr('test_np2.npy', data=bio.getbuffer().tobytes())

    with np.load(filename) as loaded_npz:
        print(np.array_equal(test_np1, loaded_npz['test_np1']))
        print(np.array_equal(test_np2, loaded_npz['test_np2']))
finally:
    os.remove(filename)

Solution 3:[3]

Not a real solution here but I'd like to share a way to add array to a new nyz file.

import numpy as np

array_dict = {'new_array': new_array}
for k in old_npz_file:
    array_dict[k] = old_npz_file[k]

np.savez(file_path, **array_dict)

I think you could say it's one way to "add" array.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Tonsic
Solution 3 Alexandar Zeng