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

Change dictionary values from $ to float value. Python Selenium

I have scraped 3 different websites and obtained 3 different values with corresponding keys.
I want to divide these values by 2 before I pass them through Venmo to make a request. However, I have not figured out a way to do this in a way that will result in halving these amounts.

My current dictionary outputs as such:

{'Water': '$0.00', 'Electric': '$42.78', 'Gas': '$272.65'}

At the end of the website scraping I was thinking to grab these keys and divide them by a dictionary that looks like this:

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

{'Water': '2', 'Electric': '2', 'Gas': '2'}

Before I do so, I have to pass the first dictionary as float values.

Any ideas as to what the best way to do this is after having the dictionary established?

Thanks!

>Solution :

You can chop off the first character to remove the dollar sign, and then transform the sliced string into a float:

data = {'Water': '$0.00', 'Electric': '$42.78', 'Gas': '$272.65'}
divisors = {'Water': '2', 'Electric': '2', 'Gas': '2'}
result = {key: float(value[1:]) / float(divisors[key]) for key, value in data.items()}

print(result) # Prints {'Water': 0.0, 'Electric': 21.39, 'Gas': 136.325}
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