DilithiumMatrix
DilithiumMatrix

Reputation: 18637

use variables as key names when using numpy savez

After loading an npz file, I like being able to access arrays with keys, e.g.:

KEY1  = "names"
file  = np.load(npzFilename)
data  = file[KEY1]

But you have to manually force this when you save, i.e.:

np.savez(npzFilename, names=names)

Is there anyway to set the NPZ dictionary key using a variable? i.e. something like

np.savez(npzFilename, names, key=KEY1)

Upvotes: 3

Views: 3331

Answers (1)

JoshAdel
JoshAdel

Reputation: 68692

Using a dictionary you could do:

vals_to_save = {KEY1:names}
np.savez(npzFilename, **vals_to_save)

where you could set up the dict vals_to_save programatically as desired.

Upvotes: 5

Related Questions