Python Import Text Array with Numpy
Question: I have a text file that looks like this:

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

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

The resulting unpacked lists should be, for example:

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

I tried

`data1, data2, data3, data4 = np.genfromtxt('data.txt', dtype=None,
delimiter='\t', unpack=True)`

but `data2` and `data3` are lists containing 'nan'.


Answer: Potential approach for given data, however not using numpy:

    
    
    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
    

Gives

    
    
    "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]"
    

