data type error while saving file to csv in python
<p>I am simply trying to save data to a csv file in python using numpy.</p>

<p>This is what I am doing:</p>

<pre><code>np.savetxt('data.csv', array, delimiter=',', fmt='%.4f')
</code></pre>

<p>however I am getting a following error </p>

<pre><code>Mismatch between array dtype ('&lt;U1') and format specifier ('%.4f')
</code></pre>

<p>what is this dtype and what does it mean?
Any help would be appreciated </p>

<p>The <a href="http://docs.scipy.org/doc/numpy/reference/generated/numpy.dtype.html" rel="nofollow"><code>dtype</code></a> of an np.array is its data type. In this case, <code>'&lt;U1'</code> stands for unsigned integer data of width 1 byte, aka an <code>unsigned char</code> in c-style languages. This being integral, it is incompatible with the <code>'%.4f'</code> <a href="https://docs.python.org/2.4/lib/typesseq-strings.html" rel="nofollow">format specifier</a>. Instead, use something like <code>'%u'</code>.</p>

<pre><code>np.savetxt('data.csv', array, delimiter=',', fmt='%u')
</code></pre>

<p>If you really want your data formatted as floating point values in your csv, you can <a href="http://docs.scipy.org/doc/numpy/reference/generated/numpy.ndarray.astype.html" rel="nofollow">cast</a> the array to float like so:</p>

<pre><code>np.savetxt('data.csv', array.astype(float), delimiter=',', fmt='%.4f')
</code></pre>

python csv numpy