2

I am using the following link to convert a array of string to array of float Convert String to float array

The data that I am getting is in a weird format

535.    535.    535.    534.68  534.68  534.68

Although numpy is able to convert the string array to float but some other is failing when data is in the format 535.

Is there a way to convert all 535. to 535.00 in one go.

I am using the following code for conversions

import numpy as np
strarray = ["535.","535.","534.68"]
floatarray = np.array(filter(None,strarray),dtype='|S10').astype(np.float)
print floatarray
Community
  • 1
  • 1
Shakti
  • 2,013
  • 8
  • 27
  • 40
  • the data do you get in by any chance from a txt file before you put it in a string array ? (535. and 535.00 are the same in calculations so your problem is in printing isn't it ?) – Félix Cantournet Dec 08 '12 at 02:33

1 Answers1

1

Convert the the strings to float128. Try this:

import numpy as np
strarray = ["535.","535.","534.68"]
floatarray = np.array(filter(None,strarray),dtype='|S10').astype(np.float128)
print floatarray

Output:

[ 535.0  535.0  534.68]

Or use the recommended longdouble:

import numpy as np
strarray = ["535.","535.","534.68"]
floatarray = np.array(filter(None,strarray),dtype='|S10').astype(np.longdouble)
print floatarray

Output:

[ 535.0  535.0  534.68]
  • float128 is not working on pydev python 2.7, not sure what is the problem but np.longdouble is working fine – Shakti Dec 08 '12 at 02:35