2

I have a NumPy array that I'm trying to write to a file

outfile.write(str(myarray))

But, I get something that looks like this:

[    4.275000000e01   2.345000000e01  3.2135000000e-02    ]

My requirements are:

  1. To write the data within the array (and not the array braces [])
  2. To write the data with a certain number of trailing decimal places (say 6)

Normally, #2 would not be that hard, but I'm not sure how to handle it in conjunction with #1.

I also want to write the array on one line as a row. like this:

4.27500000  2.34500000  0.03213500000
1

3 Answers 3

2

savetxt does the equivalent of:

In [631]: x=np.array([4.275000000e01,   2.345000000e01,  3.2135000000e-02])

In [632]: '%10.6f %10.6f %10.6f'%tuple(x)
Out[632]: ' 42.750000  23.450000   0.032135'

In [642]: outfile.write((' '.join(['%10.6f ']*x.size)+'\n\n') % tuple(x))
42.750000   23.450000    0.032135

It turns your one-dimensional array into a tuple and passes it off to the format statement, with a format specification for each item in the array.

Sign up to request clarification or add additional context in comments.

2 Comments

Yes! Thank you!!! I'm really not sure why this needs to be so difficult. In FORTRAN, this would be trivial.
In FORTRAN, at least the versions I first learned, you HAD to be explicit about the format. It doesn't seem as trivial in Python because we normally don't have to deal with those details.
2

If by writing to a file you mean text file, then use numpy.savetxt:

import numpy as np
my_array = np.random.rand(10,4)
np.savetxt('my_filenmame', my_array, fmt='%4.6f', delimiter=' ')

would write my_array to my_filename with up to six decimal digits leaving a white space in between them.

After the OP's edit: if you want to write the whole array to one line, make sure your array has only one row:

np.savetxt('my_filenmame', my_array.reshape(1,rows*cols), fmt='%4.6f', delimiter=' ')

where in this case rows=10 and cols=4. Or use ravel or flatten (see also Harpal's answer, although that does not answer the decimal precision issue)

np.savetxt('my_filenmame', my_array.ravel(), fmt='%4.6f', delimiter=' ', newline=' ')

6 Comments

Thanks, that does seem to help... Now, is there a way to print out the list/array on one row in my file?
What do you mean by "print out the list/array on one row in my file". Do you want to read only one line from the file and print it?
I have a long array that I want to print out on one line in my output file (see edit in OP). I don't want one number per line, I want all the numbers on one line. @romeric
Okay, then what is the shape of your array? If it is a one-dimensional array i.e. only one row savetxt should take care of that for you, otherwise use either ravel or flatten to flatten the the array and then write to file.
My array has shape (51,). Your solution doesn't seem to work for me. The savetxt command puts out each value of the array in a new line. ravel or flatten don't seem to do anything, I guess because I already have a 1D array.
|
1

Try this:

>>> a = np.array([[1,2,3],[1,2,3],[1,2,3]])
>>> a
array([[1, 2, 3],
       [1, 2, 3],
       [1, 2, 3]])
>>> b = a.flatten()
>>> b
array([1, 2, 3, 1, 2, 3, 1, 2, 3])
>>> >>> np.savetxt("array.txt",b,delimiter=' ',newline=' ')

Edited to write on a single line

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.