负值错误"could not convert string to float:"



你好,我得到了"无法将字符串转换为浮点:";错误我想我需要使用replace命令。我是个新手。这就是我无法编辑代码的原因。:(

def net_both(stoploss_value, takeprofit_value, wait):
wait.until(EC.visibility_of_element_located((By.CLASS_NAME, "additional_percent_value")))
try:
time.sleep(.5)
check = driver.find_elements_by_class_name("additional_percent_value")[0]
check.find_element_by_xpath('./span[contains(@class, "neg")]')
negative = True
except NoSuchElementException:
negative = False
if negative:
net_profit = driver.find_elements_by_class_name("additional_percent_value")[0].text.split(" %")
net_value = -float(net_profit[0].replace('−', ''))
profits.update({-net_value: ["Stoploss:", stoploss_value, "Take Profit:", takeprofit_value]})
print(colored(f'Net Profit: -{net_value}% --> Stoploss: {stoploss_value}, Take Profit: {takeprofit_value}', 'red'))

尝试:

  • 删除.text.split(' %')
  • float(net_profit.replace('−', '-').rstrip('%'))替换-float(net_profit[0])
if negative:
net_profit = driver.find_elements_by_class_name("additional_percent_value")[0]
net_value = float(net_profit.replace('−', '-').rstrip('%'))
profits.update({-net_value: ["Stoploss:", stoploss_value, "Take Profit:", takeprofit_value]})

最新更新