Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
192 views
in Technique[技术] by (71.8m points)

selenium - How do I convert dollar amount from a text to numbers in python?

I'm trying to get the balance amount from a website and it prints out the balance but it contains "$0.00". I want to be able to use the balance amount to use other functions such as adding it with other ones, subtracting from it or less than or equal to functions. I think I have to first convert it from text into numbers but using "int()" does not work form me, and I think it is because it contains "$". I am new to programming and still have lots to learn. I'm using python, pycharm and selenium.

Example: ... balance = "xpath".text print(balance)

question from:https://stackoverflow.com/questions/65651528/how-do-i-convert-dollar-amount-from-a-text-to-numbers-in-python

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Yes int() doesn't work for you because you have a dollar sign, and the number is float

money = '$156.99'
#if the dollar sign is in front
balance = float(money[1:])

money = '123.05$'
#if the dollar sign is in the end
balance = float(money[:-1])

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...