taking mean of data read in 'error cannot perform reduce with flexible type' python
Question: I am trying to read in data from a text file and take the mean of this data,
but I am getting the error cannot perform reduce with flexible type. I dont
know why, can somebody please help. here is my code.

    
    
    right=open('01phi.txt','r').readlines()  
    right=str(right) 
    a=np.asarray(right)
    b=np.mean(a)
    print b
    

**EDIT** : I am now using this line `right=np.genfromtxt('01phi.txt')` but it
produces this error `Line #10 (got 3 columns instead of 7)` as this line in
the array is not as big. [Using genfromtxt to import csv data with missing
values in numpy](http://stackoverflow.com/questions/3761103/using-genfromtxt-
to-import-csv-data-with-missing-values-in-numpy) this link tells my how to
ignore the bottom line or how to fill it in but both of these methods would
sque the mean. is there a way to get around this?


Answer: `right` is a string. `np.asarray(some_string)` returns an array with string
dtype. NumPy's `np.mean` function raises a TypeError when passed an array of
string dtype.

    
    
    In [29]: np.asarray('1 2 3')
    Out[29]: 
    array('1 2 3', 
          dtype='|S5')
    In [31]: np.mean(a)
    TypeError: cannot perform reduce with flexible type
    

Instead, use
[np.genfromtxt](http://docs.scipy.org/doc/numpy/reference/generated/numpy.genfromtxt.html)
or
[np.loadtxt](http://docs.scipy.org/doc/numpy/reference/generated/numpy.loadtxt.html#numpy.loadtxt)
to load an array from a text file:

    
    
    a = np.genfromtxt(filename, ...)
    

