I am new to Python. I am trying to store a 4d numpy array in a file (done it) and read the values of this file (there's the problem). The code is:
import numpy as np
Lx=6;Ly=6,Lz=2
mat=np.zeros((Lx,Ly,Lz,3),dtype=np.float)
mat=np.random.rand(Lx,Ly,Lz,3)
outfile=open("config.txt","w")
for i in range(0,Lx):
for j in range(0,Ly):
for k in range(0,Lz):
print(mat[i,j,k,0], mat[i,j,k,1], mat[i,j,k,2],file=outfile)
outfile.close()
mnew=np.zeros((Lx,Ly,Lz,3),dtype=np.float)
infile=open("config.txt","r")
for i in range(0,Lx):
for j in range(0,Ly):
for k in range(0,Lz):
infile.read(mnew[i,j,k,0], mnew[i,j,k,1], mnew[i,j,k,2])
I get the error:
infile.read(mnew[i,j,k,0], mnew[i,j,k,1], mnew[i,j,k,2]) TypeError: read() takes at most 1 argument (3 given)
but I don't know how to fix it
Thanks, M
readfunction herereadfunction of files reads from files, but it does not read directly into variables. If you insist on using read like such without using propernumpyfunctions for loading and saving files, what I think you want is to read the lines usingreadlinesand insert into variables