Dismiss
Announcing Stack Overflow Documentation

We started with Q&A. Technical documentation is next, and we need your help.

Whether you're a beginner or an experienced developer, you can contribute.

Sign up and start helping → Learn more about Documentation →

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
share|improve this question
    
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 8 '12 at 2:33
up vote 1 down vote accepted

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]
share|improve this answer
    
float128 is not working on pydev python 2.7, not sure what is the problem but np.longdouble is working fine – Shakti Dec 8 '12 at 2:35

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.