I have this list bhs created this way:
#Alotting Black Holes at z=6
bhs=[0]*1000
for i in tqdm(range(0,1000),position=0, leave=True):
if len(mass_array1[i])!=0:
bhs[i]=np.zeros(len(mass_array1[i]))
else:
bhs[i]=np.zeros(1)
for j in range (len(mass_array1[i])):
bhs[i][j]=np.random.lognormal(np.log(MbhthShimasaku(mass_array1[i],6)[j]),np.log(5))
I need to save the result in a text file. I have tried numpy.savetxt, pickle.dump and open():
open()
with open("bhs.txt", 'w') as file:
for row in bhs:
s = " ".join(map(str, row))
file.write(s+'\n')
#Result .txt file:
0.0
0.0
0.0
0.0
1937651.7861915156 246221.20328840986 226756.87389065413
0.0
0.0
numpy.savetxt()
bhs=np.array(bhs)
np.savetxt('bhs.txt',bhs,fmt='%s')
#Result .txt file:
[0.]
[0.]
[0.]
[0.]
[26447480.89508711 1097038.92200952 971383.67441455]
[0.]
[0.]
[0.]
[0.]
[0.]
pickle
bhs.append(bhs)
tupleA=tuple(bhs)
filename = 'bhs.p'
with open(filename, 'wb') as filehandler:
pickle.dump(tupleA, filehandler)
#Result .p file
array([0.]), array([0.]), array([0.]), array([0.]), array([0.]), array([0.]), array([0.]), array([0.]), array([0.]), array([0.]), array([1937651.78619152, 246221.20328841, 226756.87389065])
I am unable to get back the original array/list from all these saved files. When I try to use any of these loaded lists, I get some kind of error:
np.loadtxt
could not convert string to float: '[0.]'
open()
my_file = open("bhs.txt", "r")
content = my_file.read()
content_list = content.split(",")
my_file.close()
print(content_list)
[0.]\n[0.]\n[26447480.89508711 1097038.92200952 971383.67441455]\n[0.]\n[0.]\n[0.]\n[0.]\n[0.]\n[0.]\n[0.]\n[0.]\n
Sample of bhs as a list
array([1461403.98258597]), array([0.]), array([0.]), array([0.]), array([0.]), array([0.]), array([0.]), array([0.]), array([0.]), array([0.]), array([0.]), array([0.]), array([0.]), array([0.]), array([0.]), array([0.]), array([0.]), array([0.]), array([0.]), array([0.]), array([0.]), array([0.]), array([0.]), array([0.]), array([0.]), array([0.]), array([0.]), array([0.]), array([0.]), array([0.]), array([0.]), array([0.]), array([0.]), array([0.]), array([0.]), array([0.]), array([0.]), array([0.]), array([0.]), array([0.]), array([0.]), array([0.]), array([0.]), array([0.]), array([0.]), array([26447480.89508711, 1097038.92200952, 971383.67441455]),
How can I say my multidimensional list so that I can get back exactly what I started with?
Extra: mass_array1 file
https://drive.google.com/file/d/1Kdmv1fcbDelEzGmi4BOE4HjUbM7Cg23b/view?usp=sharing
And this is how I import it into python:
You need to unzip the file into a folder first.
dirlist=["bh2e10"]
import time
mass_array1=[0]*1000
#print(mass_array)
#read all the files
for i,X in enumerate(dirlist):
exec('filelist=glob.glob("%s/test*.dat")'%(X))
#exec("mass_array%s=[]"%X)
initial_mass=[]
for j,Y in tqdm(enumerate(filelist),position=0, leave=True, total=1000):
Y=Y.replace(os.sep, '/')
#Z=int(Y[10:13])
Z=int(re.findall("\d+", Y)[2])
#print(Z)
mass_array1[Z]=[]
#print('i=',Z,end="\r")
#print('i=',Z,end="\r")
exec("initial_partial=np.loadtxt('%s',max_rows=1)"%(Y))
exec("initial_mass=np.append(initial_mass,initial_partial)")
exec("mass_partial=np.loadtxt('%s',skiprows=1)"%(Y))
mass_array1[Z]=np.append(mass_partial,mass_array1[Z])
#mass_array1[Z]=mass_partial