How to write numpy arrays to .txt file, starting at a certain line?
<p>I need to write 3 numpy arrays into a txt file. The head of the file looks like that:</p>

<pre><code>#Filexy
#time  operation1 operation2
</code></pre>

<p>The numpy arrays look like the following:</p>

<pre><code>time = np.array([0,60,120,180,...])
operation1 = np.array([12,23,68,26,...)]
operation2 = np.array([100,123,203,301,...)]
</code></pre>

<p>In the end, the .txt file should look like this (delimiter should be a tab):</p>

<pre><code>#Filexy
#time  operation1 operation2
0   12   100
60  23    123
120  68   203
180  26   301
..  ...   ...
</code></pre>

<p>I tried it with "numpy.savetxt" - but I did not get the format that I want.</p>

<p>Thank you very much for your help!</p>

<p>I'm not sure what you tried, but you need to use the <code>header</code> parameter in <code>np.savetxt</code>. Also, you need to concatenate your arrays properly. The easiest way to do this is to use <a href="http://docs.scipy.org/doc/numpy/reference/generated/numpy.c_.html" rel="nofollow"><code>np.c_</code></a>, which forces your 1D-arrays into 2D-arrays, and then concatenates them the way you expect.</p>

<pre><code>&gt;&gt;&gt; time = np.array([0,60,120,180])
&gt;&gt;&gt; operation1 = np.array([12,23,68,26])
&gt;&gt;&gt; operation2 = np.array([100,123,203,301])
&gt;&gt;&gt; np.savetxt('example.txt', np.c_[time, operation1, operation2],
               header='Filexy\ntime  operation1 operation2', fmt='%d',
               delimiter='\t')
</code></pre>

<p><code>example.txt</code> now contains:</p>

<pre><code># Filexy
# time  operation1 operation2
0   12  100
60  23  123
120 68  203
180 26  301
</code></pre>

<p>Also note the usage of <code>fmt='%d'</code> to get integer values in the output. <code>savetxt</code> will save as float by default, even for an integer array.</p>

<p>Regarding the delimiter, you just need to use the <code>delimiter</code> argument. It's not clear here, but there are, in fact, tabs between the columns. For instance, <code>vim</code> shows me tabs using dots:</p>

<pre><code># Filexy
# time  operation1 operation2
0Â·  12Â· 100
60Â· 23Â· 123
120Â·68Â· 203
180Â·26Â· 301
</code></pre>

<p><strong>Addendum:</strong></p>

<p>If you want to add headers <em>and</em> add an extra line before the arrays, you are better off creating a custom header, complete with your own comment characters. Use the <code>comment</code> argument to prevent <code>savetxt</code> from adding extra <code>#</code>'s.</p>

<pre><code>&gt;&gt;&gt; extra_text = 'Answer to life, the universe and everything = 42'
&gt;&gt;&gt; header = '# Filexy\n# time operation1 operation2\n' + extra_text
&gt;&gt;&gt; np.savetxt('example.txt', np.c_[time, operation1, operation2],     
               header=header, fmt='%d', delimiter='\t', comments='')
</code></pre>

<p>which produces</p>

<pre><code># Filexy
# time operation1 operation2
Answer to life, the universe and everything = 42
0   12  100
60  23  123
120 68  203
180 26  301
</code></pre>

python numpy writetofile