python-3.x - 如何在python中将美元金额从文本转换为数字?
问题描述
我正在尝试从网站获取余额,它会打印出余额,但它包含“$ 0.00”。我希望能够使用余额来使用其他功能,例如将其与其他功能相加,从中减去或小于或等于功能。我想我必须首先将它从文本转换为数字,但使用“int()”对我不起作用,我认为这是因为它包含“$”。我是编程新手,还有很多东西要学。我正在使用 python、pycharm 和 selenium。
示例: ... balance = "xpath".text print(balance)
解决方案
是的 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])
推荐阅读
- c# - 将动态变量传递给c#中的函数
- typescript - 从纯 JavaScript 导入时的自动路径调整
- python - “NoneType”对象没有属性“raise_exception_on_not_ok_status”
- .net-core - Azure Devops - dotnetcore 构建始终失败 - 进程无法启动
- javascript - 声明变量是否会产生间接费用?
- php - How to return number rows affected on multiple inserts with Mysqli Php?
- cassandra - 重新启动 cassandra 节点时遇到令牌配置异常
- vb.net - visual basic 计算机故障排除程序
- nativescript - Nativescript 过渡动画在 android 设备上不稳定
- json - 您应该如何引用 JSON 对象所遵循的 JSON 模式?