Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Extract the price from a string yields ValueError: could not convert string to float: '.95.00'

I’m trying to extract a price value as float in python. The price comes as Rs.95.00

one = float(''.join(c for c in price_laughs if (c.isdigit() or c =='.')))
print(one)

I tried to extract the price using the following code, but since there’s a ‘.’ at the beginning, i’m unable to get the value as 95.00. How can i extract the price as a float value.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

If the value is always going to contain Rs., one way is to split one time from left passing maxsplit=1 then to get the last value.

>>> v='Rs.95.00'
>>> float(v.split('.', 1)[-1])
95.0
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading