Hi, I have generated an array of random numbers and I'm trying to then write this array to a .txt file but the code I have written doesn't seem to do this correctly. Any help on this would be great. Here is what I have so far:

import numpy as N
import random

def main():
n=10
size=1500
initial=N.zeros([n,3], dtype=N.float64)
filename="initialpositions.txt"

for i in range(n):
for j in range(size):
initial[i,j]=random.randrange(0,size)
file=open(filename,'w')
file.write(initial)
file.close()

import numpy
import random

def main():
    n=10
    size = 1500
    initial = numpy.zeros([n,3], dtype=numpy.float64)
    filename = initialpositions.txt

    for i in range(n):
        for j in range(0,3):
            initial[i,j]=random.randrange(0,size)
    file = open(filename,'w')
    file.write(initial)
    file.close()

Edited 5 Years Ago by ytrewq1: n/a

Firstly don't use variable names as file/list etc or something like that, use ifile/afile or ...

initial.txt is not variable -> it is file name, use "example.txt" --> " " or ' '

filename = "initialpositions.txt"

To write this to file:

val.write("\n".join(str(elem) for elem in initial))
[  745.  1306.   630.]
[ 1457.   836.  1255.]
[ 574.  453.  130.]
[ 1287.  1430.  1430.]
[ 1386.   896.  1341.]
[ 1291.  1453.   276.]
[ 1147.   231.   340.]
[ 418.  585.  151.]
[ 560.  118.  954.]
[  819.   742.  1166.]
for line in initial:
        #print line
        val.write(" ".join(str(elem) for elem in line) + "\n")

join tutorial: http://www.java2s.com/Code/Python/String/Joiningstrings.htm

Edited 5 Years Ago by -ordi-: n/a

thanks that was really helpful but now I'm struggling to recreate the array from the file containing just the numbers. I'm unsure of how to access the numbers in the file so that some sort of loop can be written to put them all back into an array.

You can use Pickle

import pickle
import numpy
X = numpy.matrix(numpy.zeros([801,801]))

print 'Before pickle'
print '-'*30
print X
print '-'*30

def write(data, outfile):
        f = open(outfile, "w+b")
        pickle.dump(data, f)
        f.close()

def read(filename):
        f = open(filename)
        data = pickle.load(f)
        f.close()
        return data

if __name__ == "__main__":
        some_data = X
        write(some_data, "temp.file")
        read_data = read("temp.file")
        print 'After pickle'
        print '-'*30
        print read_data
        print '-'*30

"""Output-->
Before pickle
------------------------------
[[ 0.  0.  0. ...,  0.  0.  0.]
 [ 0.  0.  0. ...,  0.  0.  0.]
 [ 0.  0.  0. ...,  0.  0.  0.]
 ..., 
 [ 0.  0.  0. ...,  0.  0.  0.]
 [ 0.  0.  0. ...,  0.  0.  0.]
 [ 0.  0.  0. ...,  0.  0.  0.]]
------------------------------
After pickle
------------------------------
[[ 0.  0.  0. ...,  0.  0.  0.]
 [ 0.  0.  0. ...,  0.  0.  0.]
 [ 0.  0.  0. ...,  0.  0.  0.]
 ..., 
 [ 0.  0.  0. ...,  0.  0.  0.]
 [ 0.  0.  0. ...,  0.  0.  0.]
 [ 0.  0.  0. ...,  0.  0.  0.]]
------------------------------
"""

Edited 5 Years Ago by snippsat: n/a

This article has been dead for over six months. Start a new discussion instead.