user2300369 - 4 months ago 9

Python Question

I have a file text with data in a following format:

rubbish & 3.97& 3.83& 3.95& 3.83& 3.82

rubbish & 4.92& 4.81& 4.88& 4.81& 4.81

rubbish & 5.90& 5.66& 5.88& 5.66& 5.66

rubbish &--- & 6.05& 6.14& 6.05& 6.05

rubbish & 6.42& 6.26& 6.46& 6.26& 6.26

rubbish &--- & 6.56& 6.63& 6.56& 6.56

And I want to read them into numpy.ndarray object so that numbers are transformed into floating point number object while the

`---`

`import numpy as np`

wejscie = open('data.dat', 'r').readlines()

def fun1(x):

print x

if x.strip() == '---':

return str(x)

else:

return float(x)

dane = np.array([map(fun1, linijka.split('&')[1:]) for linijka in wejscie])

So is it possible to have numpy.ndarray object containing data of various types?

Answer

The problem isn't with `fun1`

, it's with trying to insert elements of differing types into a numpy array.

Consider the following:

```
>>> a = numpy.array([1])
>>> numpy.append(a,2)
array([1, 2])
>>> numpy.append(a,'b')
array(['1', 'b'],
dtype='<U11')
```

You may find this helpful Store different datatypes in one NumPy array?

Source (Stackoverflow)

Comments