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()

Recommended Answers

All 5 Replies

can you pls put your code into the code tags
:)

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()

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

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.]]
------------------------------
"""
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.