How can I convert strings which can denote decimal or rational numbers to floats
>>> ['0.1234', '1/2'] ['0.1234', '1/2']
I’d want [0.1234, 0.5].
eval is what I was thinking but no luck:
>>> eval('1/2') 0
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
I’d parse the string if conversion fails:
Generally using eval is a bad idea, since it’s a security risk. Especially if the string being evaluated came from outside the system.