Convert import string to float with numpy's loadtext
<p>I'm attempting to import text from a flat file and to convert it to float values within a single line. I've seen <a href="http://stackoverflow.com/questions/8420143/valueerror-could-not-convert-string-to-float-id">this post</a> which has the same error, but I haven't found which characters are invalid in my input file. Or do I have a syntax error?</p>

<p>Import as a string an print the result:</p>

<pre><code>data = np.loadtxt(file, delimiter='\t', dtype=str)
print(data[0:2])
... 
[["b'Time'" "b'Percent'"]
 ["b'99'" "b'0.067'"]]
</code></pre>

<p>Attempt to import as float:</p>

<pre><code># Import data as floats and skip the first row: data_float
data_float = np.loadtxt(data, delimiter='\t', dtype=float, skiprows=1)
</code></pre>

<p>It throws the following error:</p>

<pre><code>Traceback (most recent call last):
  File "&lt;stdin&gt;", line 1, in &lt;module&gt;
    data_float = np.loadtxt(data, delimiter='\t', dtype=float, skiprows=1)
  File "&lt;stdin&gt;", line 848, in loadtxt
    items = [conv(val) for (conv, val) in zip(converters, vals)]
  File "&lt;stdin&gt;", line 848, in &lt;listcomp&gt;
    items = [conv(val) for (conv, val) in zip(converters, vals)]
ValueError: could not convert string to float: b'["b\'99\'" "b\'0.067\'"]'
</code></pre>

<p>By the way, I've also seen <a href="http://stackoverflow.com/questions/6269765/what-does-the-b-character-do-in-front-of-a-string-literal">this post</a> which explains the <code>b</code> character, but I don't think that's the issue.</p>

<p>An additional troubleshooting step as suggested by the first answer:</p>

<pre><code>data = np.loadtxt(file, delimiter="\tb'", dtype=str)
</code></pre>

<p>Returns:</p>

<pre><code>array(["b'Time\\tPercent'", "b'99\\t0.067'", "b'99\\t0.133'",
       "b'99\\t0.067'", "b'99\\t0'", "b'99\\t0'", "b'0\\t0.5'",
       "b'0\\t0.467'", "b'0\\t0.857'", "b'0\\t0.5'", "b'0\\t0.357'",
       "b'0\\t0.533'", "b'5\\t0.467'", "b'5\\t0.467'", "b'5\\t0.125'",
       "b'5\\t0.4'", "b'5\\t0.214'", "b'5\\t0.4'", "b'10\\t0.067'",
       "b'10\\t0.067'", "b'10\\t0.333'", "b'10\\t0.333'", "b'10\\t0.133'",
       "b'10\\t0.133'", "b'15\\t0.267'", "b'15\\t0.286'", "b'15\\t0.333'",
       "b'15\\t0.214'", "b'15\\t0'", "b'15\\t0'", "b'20\\t0.267'",
       "b'20\\t0.2'", "b'20\\t0.267'", "b'20\\t0.437'", "b'20\\t0.077'",
       "b'20\\t0.067'", "b'25\\t0.133'", "b'25\\t0.267'", "b'25\\t0.412'",
       "b'25\\t0'", "b'25\\t0.067'", "b'25\\t0.133'", "b'30\\t0'",
       "b'30\\t0.071'", "b'30\\t0'", "b'30\\t0.067'", "b'30\\t0.067'",
       "b'30\\t0.133'"], 
      dtype='&lt;U16')
</code></pre>

<p>Thanks to everyone who took a look at my question. I restarted IPython and was now able to execute the same code without any problems. Here's the code that worked which is identical to above.     </p>

<pre><code>data_float = np.loadtxt(file, delimiter='\t', dtype=float, skiprows=1)
</code></pre>

<p>Result:</p>

<pre><code>In [1]: data_float
Out[1]: 
array([[  9.90000000e+01,   6.70000000e-02],
       [  9.90000000e+01,   1.33000000e-01],
       [  9.90000000e+01,   6.70000000e-02],
       [  9.90000000e+01,   0.00000000e+00],
       [  9.90000000e+01,   0.00000000e+00],
       [  0.00000000e+00,   5.00000000e-01],
       [  0.00000000e+00,   4.67000000e-01],
       [  0.00000000e+00,   8.57000000e-01],
       [  0.00000000e+00,   5.00000000e-01],
       [  0.00000000e+00,   3.57000000e-01],
       [  0.00000000e+00,   5.33000000e-01],
       [  5.00000000e+00,   4.67000000e-01],
       [  5.00000000e+00,   4.67000000e-01],
       [  5.00000000e+00,   1.25000000e-01],
       [  5.00000000e+00,   4.00000000e-01],
       [  5.00000000e+00,   2.14000000e-01],
       [  5.00000000e+00,   4.00000000e-01],
       [  1.00000000e+01,   6.70000000e-02],
       [  1.00000000e+01,   6.70000000e-02],
       [  1.00000000e+01,   3.33000000e-01],
       [  1.00000000e+01,   3.33000000e-01],
       [  1.00000000e+01,   1.33000000e-01],
       [  1.00000000e+01,   1.33000000e-01],
       [  1.50000000e+01,   2.67000000e-01],
       [  1.50000000e+01,   2.86000000e-01],
       [  1.50000000e+01,   3.33000000e-01],
       [  1.50000000e+01,   2.14000000e-01],
       [  1.50000000e+01,   0.00000000e+00],
       [  1.50000000e+01,   0.00000000e+00],
       [  2.00000000e+01,   2.67000000e-01],
       [  2.00000000e+01,   2.00000000e-01],
       [  2.00000000e+01,   2.67000000e-01],
       [  2.00000000e+01,   4.37000000e-01],
       [  2.00000000e+01,   7.70000000e-02],
       [  2.00000000e+01,   6.70000000e-02],
       [  2.50000000e+01,   1.33000000e-01],
       [  2.50000000e+01,   2.67000000e-01],
       [  2.50000000e+01,   4.12000000e-01],
       [  2.50000000e+01,   0.00000000e+00],
       [  2.50000000e+01,   6.70000000e-02],
       [  2.50000000e+01,   1.33000000e-01],
       [  3.00000000e+01,   0.00000000e+00],
       [  3.00000000e+01,   7.10000000e-02],
       [  3.00000000e+01,   0.00000000e+00],
       [  3.00000000e+01,   6.70000000e-02],
       [  3.00000000e+01,   6.70000000e-02],
       [  3.00000000e+01,   1.33000000e-01]])
</code></pre>

python python-3.x ipython