首页 > 解决方案 > 如何在python中将美元金额从文本转换为数字?

问题描述

我正在尝试从网站获取余额,它会打印出余额,但它包含“$ 0.00”。我希望能够使用余额来使用其他功能,例如将其与其他功能相加,从中减去或小于或等于功能。我想我必须首先将它从文本转换为数字,但使用“int()”对我不起作用,我认为这是因为它包含“$”。我是编程新手,还有很多东西要学。我正在使用 python、pycharm 和 selenium。

示例: ... balance = "xpath".text print(balance)

标签: python-3.xseleniumpycharminteger

解决方案


是的 int() 对你不起作用,因为你有一个美元符号,而且数字是浮动的

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])

推荐阅读