title: How to write a numpy matrix in a text file - python
body: <p>Suppose I am getting a numpy matrix from some calculation. Here is my numpy matrix 'result1'::</p>
  
  <pre><code>    result1=
      [[   1.         0.         0.         0.00375   -0.01072   -0.      -1000.     ]
       [   2.         3.         4.         0.        -0.004    750.         0.     ]
       [   3.         3.         0.         0.         0.      -750.      1000.     ]]
  </code></pre>
  
  <p>Now I want to write this matrix in a text file named 'result.txt'. For this, I wrote the following code::</p>
  
  <pre><code>np.savetxt('result.txt', result1, fmt='%.2e')
  </code></pre>
  
  <p>But it is giving me all the elements of the matrix in one row.</p>
  
  <pre><code>    1.00e+00 0.00e+00 0.00e+00 3.75e-03 -1.07e-02 -1.14e-13 -1.00e+032.00e+00 3.00e+00 4.00e+00 0.00e+00 -4.00e-03 7.50e+02 0.00e+003.00e+00 3.00e+00 0.00e+00 0.00e+00 0.00e+00 -7.50e+02 1.00e+03
  </code></pre>
  
  <p>I want to write the matrix in the text file in the proper matrix format. How can I do this? I used keyword newline='\n' or newline='',but the result is same.</p>
  
  <p>Thanks in advance...  </p>
  
  <p>=======</p>
  
  <p>This edited part is for @Warren</p>
  
  <p>try this one:</p>
  
  <pre><code>&gt;&gt;&gt; import numpy as np
  &gt;&gt;&gt; mat=np.matrix([[1, 2, 3],[4, 5, 6],[7, 8, 9]])
  &gt;&gt;&gt; mat
  matrix([[1, 2, 3],
          [4, 5, 6],
          [7, 8, 9]])
  &gt;&gt;&gt; np.savetxt('text.txt',mat,fmt='%.2f')
  </code></pre>
  
  <p>in my text.txt file, I am getting:</p>
  
  <p>1.00 2.00 3.004.00 5.00 6.007.00 8.00 9.00</p>