1

I am trying to convert the following string into float: u'"0.5"' but it seems that it does not work. I am trying to use float(str1) and I am receiving the following error:

ValueError: could not convert string to float: "0.5"

It seems is the way that I have stored the string. But how can I convert it into a float properly?

3
  • 3
    Don't you need to remove the rabbit's ears yourself? Commented Feb 6, 2017 at 12:19
  • 2
    float(str1.strip('"')) Commented Feb 6, 2017 at 12:20
  • float(ast.literal_eval(u'"0.5"')) Commented Feb 6, 2017 at 12:23

2 Answers 2

1

You try to convert the " " to float, which isn't possible.

This is a step by step code of what you should do :

unicode_value = u'"0.5"'

string_value = str(unicode_value)

float_value = float(string_value.strip('"'))

print float_value

This works without import, but you might as well use the other solution provided before by Shivkumar Kondi.

Sign up to request clarification or add additional context in comments.

Comments

0

Try this..

import ast

a = u'"0.5"'
print a,type(a)

b = ast.literal_eval(a)
print b,type(b)

c = float(b)
print c,type(c)

Output :

"0.5" <type 'unicode'>
0.5 <type 'str'>
0.5 <type 'float'>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.