Search code examples
pythonnumpynumpy-ndarrayscientific-notation

How to create a numpy array from a string containing scientific notation


I have a set of data I want to process from a text file where each line is to be a numpy array. Some are easy to convert to arrays using the fromstring method, but other have scientific notation entries within them. How would I go about converting those into a np array?

My data looks like (this would have to be one array):

0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00
  0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00
  0.00000000e+00 8.02796109e-07 0.00000000e+00 0.00000000e+00
  0.00000000e+00 0.00000000e+00 9.99999197e-01        

Solution

  • I'd do

    np.array(x.split(), dtype=np.float)

    where x is your string