From a loop I\'m getting an array. I want to save this arrays in a tempfile.
The problem is that np.savez only saves the last array from the loop.
I am not an experienced programmer, but this is the way I did it (just in case it may help someone in the future). In addition, it is the first time that I am posting here, so I apologize if I am not following some kind of standard ;)
Creating the npz file:
import numpy as np
tmp = file("C:\\Windows\\Temp\\temp_npz.npz",'wb')
# some variables
a= [23,4,67,7]
b= ['w','ww','wwww']
c= np.ones((2,6))
# a lit containing the name of your variables
var_list=['a','b','c']
# save the npz file with the variables you selected
str_exec_save = "np.savez(tmp,"
for i in range(len(var_list)):
str_exec_save += "%s = %s," % (var_list[i],var_list[i])
str_exec_save += ")"
exec(str_exec_save)
tmp.close
Loading the variables with their original names:
import numpy as np
import tempfile
tmp = open("C:\\Windows\\Temp\\temp_npz.npz",'rb')
# loading of the saved variables
var_load = np.load(tmp)
# getting the name of the variables
files = var_load.files
# loading then with their original names
for i in range(len(files)):
exec("%s = var_load['%s']" % (files[i],files[i]) )
The only difference is that the variables will become numpy variables.