writing efficiently a numpy array

Stephan stephan.schumacher at andra.fr
Wed Feb 27 14:45:02 EST 2002


Hi,

I want to write a large numpy array on an ascii file. There must be N 
numbers per line (except for the last line...). 

Does anyone knows something more efficient than the function below :

from Numeric import *
from string import *

def writeArray(file, my_array, N) :
    my_string_array=map(repr,my_array.tolist())
    tmp = []
    ind = 0
    for i in my_string_array :
    	tmp.append(i)
	if ind == N-1 :
       	tmp.append('\n')
	ind = (ind + 1)%N
    file.write(join(tmp," "))


# exemple : 

a = arange(300000)
file = open("exemple","w")
writeArray(file, a, 8)
file.close()







More information about the Python-list mailing list