I am using python 2.7. I tried to store 2d arrays in file, but it stored only recent value. Suppose if I enter values for 3 arrays which are of 4 rows and two columns then it just store recent single value which i entered for last array. I used numpy for taking input for array. I tried this code:
import numpy as np
from math import *
def main ():
i_p = input("\n Enter number of input patterns:")
out = raw_input("\n Enter number of output nodes:")
hidden = raw_input("\n Enter number of hidden layers:")
print 'Patterns:'
for p in range(0,i_p):
print "z[%d]"%p
rows=input("Enter no of rows:")
cols=input("Enter no of coloumns:")
ff=open('array.txt','w')
for r in range(0,rows):
for c in range(0,cols):
z=np.matrix(input())
ff.write(z)
np.savetxt('array.txt',z)
if __name__=="__main__":
main()
Your
np.savetxt('array.txt',z)
opens the file for a fresh write; thus it destroys anything written to that file before.
Try:
ff=open('array.txt','w')
for i in range(3):
z = np.ones((3,5))*i
np.savetxt(ff,z)
This should write 9 lines, with 5 columns
I was going to adapt your:
for r in range(0,rows):
for c in range(0,cols):
z=np.matrix(input())
np.savetxt...
But that doesn't make sense. You don't write by 'column' with savetxt
.
Go to a Python interpreter, make a simple array (not np.matrix
), and save it. Make several arrays and save those. Look at what you saved.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.