如何删除 <span id="balance">1 268</span>
中的空格并将其设为 int
代码:
Balance_afterWinORLoseBet.replace(" ", "")
Balance_afterWinORLoseBet = int(balance_new.text) #Makes new var, turns it into int.
if "10.1" in countdown_timer.text: #
Balance_afterPlaceBet.replace(" ", "")
Balance_afterPlaceBet = int(balance_old.text) #Makes new var, turns it into int.
Error
:
Balance_afterWinORLoseBet = int(balance_new.text) #Makes new var, turns it into int. ValueError: invalid literal for int() with base 10: '1 268'
最佳答案
其他答案都没有解释为什么您的代码不起作用。原因是.replace()
并没有改变原来的变量,而是返回"a copy of string s with all occurrences of substring old replaced by new" .
示例
>>> Balance_afterWinORLoseBet = "1 268"
>>> Balance_afterWinORLoseBet.replace(" ", "")
'1268'
>>> Balance_afterWinORLoseBet
'1 268'
关于Python从字符串中删除空格将其转换为int,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37906790/