writing efficiently a numpy array

Emile van Sebille emile at fenx.com
Wed Feb 27 15:05:35 EST 2002


"Stephan" <stephan.schumacher at andra.fr> wrote in message
news:MPG.16e755abd178b85e989682 at news.wanadoo.fr...
>
> 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 :
>

In what area are you looking to be more efficient?  Memory, time, or
something else?  Do you need it in ascii form for another program, or
are you saving it to be read back in?

Things you might consider are tostring if your saving for Numeric use
only, or concatenate and reshape by N and writing out row by row.

--

Emile van Sebille
emile at fenx.com

---------



> 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