Python Import Text Array with Numpy
<p>I have a text file that looks like this:</p>

<pre><code>...
5   [0, 1]  [512, 479]  991
10  [1, 0]  [706, 280]  986
15  [1, 0]  [807, 175]  982
20  [1, 0]  [895, 92]   987
...
</code></pre>

<p>Each column is tab separated, but there are arrays in some of the columns. Can I import these with <code>np.genfromtxt</code> in some way?</p>

<p>The resulting unpacked lists should be, for example:</p>

<pre><code>data1 = [..., 5, 10, 15, 20, ...]
data2 = [..., [512, 479], [706, 280], ... ] (i.e. a 2D list)
etc.
</code></pre>

<p>I tried </p>

<p><code>data1, data2, data3, data4 = np.genfromtxt('data.txt', dtype=None, delimiter='\t', unpack=True)</code></p>

<p>but <code>data2</code> and <code>data3</code> are lists containing 'nan'.</p>

<p>Potential approach for given data, however not using numpy:</p>

<pre><code>import ast

data1, data2, data3, data4 = [],[],[],[]

for l in open('data.txt'):
    data = l.split('\t')

    data1.append(int(data[0]))
    data2.append(ast.literal_eval(data[1]))
    data3.append(ast.literal_eval(data[2]))
    data4.append(int(data[3]))

print 'data1', data1
print 'data2', data2
print 'data3', data3
print 'data4', data4
</code></pre>

<p>Gives</p>

<pre><code>"data1 [5, 10, 15, 20]"
"data2 [[0, 1], [1, 0], [1, 0], [1, 0]]"
"data3 [[512, 479], [706, 280], [807, 175], [895, 92]]"
"data4 [991, 986, 982, 987]"
</code></pre>

python arrays numpy genfromtxt