为了账号安全,请及时绑定邮箱和手机立即绑定

值错误:无法在 Python 3 中将字符串转换为浮点数:''

值错误:无法在 Python 3 中将字符串转换为浮点数:''

婷婷同学_ 2022-08-02 16:22:32
我正在用Python编写一个小机器人,但我遇到了一个问题。这似乎是一个常见的问题,但我从未见过它在我所处的相同情况下被问到。好的,所以这是代码提出问题:old_values = float((removeprc(browser.find_element_by_xpath('//*[@id="draggableNavRightResizable"]/section/section[2]/section[1]/div[3]/ul/li[1]/div[2]/div[6]/span').text)))browser.find_element_by_xpath('//*[@id="draggableNavRightResizable"]/section/section[2]/section[1]/div[3]/ul/li[1]/div[2]/div[6]/span').text)是一个硒工具,用于获取网站的价值。正如您稍后将看到的,检索到的元素是一个应该与 float() 一起使用的数字 “remove prc” 是我创建的用于删除数字百分比的小函数,这里是:def removeprc(string): #removes the % from a string     string = str(string)    list = string.split('%')    string = " ".join(list)        return string这可能不是最好的方法,但当我单独测试它时,它有效。无论如何,这是我在运行整个代码时得到的loading page ...page loadedacquiring values ...values acquiredrunning eth trade-0.37Traceback (most recent call last):  File "C:\Users\pc adam\Documents\EISTI\algoprog\perso\python\fichiers\btc\ETHtradingbotV1.py", line 138, in <module>    profit = float(browser.find_element_by_xpath('/html/body/div[3]/section[16]/section[2]/section[2]/section[2]/div/div[1]/table/tbody/tr/td[15]/span').text)ValueError: could not convert string to float: ''前5行是无用的。在第6行,我打印了我试图获得的浮点()的东西。如您所见,它应该工作并且...确实如此!有时。这是最奇怪的事情,它完美地工作了一半!我在互联网上读到,如果你试图漂浮()不是数字或里面有奇怪狗屎的东西,比如空格,可能会发生这种情况。正如你所看到的,我认为这里的情况并非如此。当我尝试通过运行简化版本的程序来隔离问题时,如下所示:a = "-0.06%"def removeprc(string): #removes the % from a string     string = str(string)    list = string.split('%')    string = " ".join(list)    return stringb = float(removeprc(a))print(b)它输出-0.06并完美地工作???所以我真的被困在这里。它应该有效,但它不起作用。更糟糕的是,它有时无缘无故地起作用。当我隔离问题时,它工作正常。任何帮助将不胜感激!哦,如果你想看到整个代码,它在这里:https://github.com/Madaxuorel/proj-ethTB/blob/master/ETHtradingbotV1.py
查看完整描述

3 回答

?
素胚勾勒不出你

TA贡献1827条经验 获得超9个赞

此错误消息...


ValueError: could not convert string to float: ''

...这意味着Python解释器无法将字符串转换为浮点数。


你离得足够近了。text 方法将返回一个字符串并去掉 ,而不是您想要的 。%string.split('%')list = string.split('%')[0]


例如:


my_percentage = "99%"

my_string_num = my_percentage.split("%")[0]

print(my_string_num)

指纹:


99

此外,将仅标识单个元素,并使用文本您将获得单个字符串,因此似乎是多余的。find_element_by_xpath()string = " ".join(list)


因此,有效地,要剥离 ,将字符串转换为浮点型和打印型,您的有效代码行将是:%


print(float(browser.find_element_by_xpath('//*[@id="draggableNavRightResizable"]/section/section[2]/section[1]/div[3]/ul/li[1]/div[2]/div[6]/span').text.split("%")[0]))

更新

您仍然看到错误,因为调用代码行时,具有所需文本的元素尚未在 DOM 中呈现。作为解决方案,您需要诱导WebDriverWait,并且可以使用以下定位器策略:visibility_of_element_located()


print(float(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//*[@id='draggableNavRightResizable']/section/section[2]/section[1]/div[3]/ul/li[1]/div[2]/div[6]/span"))).text.split("%")[0]))

注意:您必须添加以下导入:


from selenium.webdriver.support.ui import WebDriverWait

from selenium.webdriver.common.by import By

from selenium.webdriver.support import expected_conditions as EC


查看完整回答
反对 回复 2022-08-02
?
守着星空守着你

TA贡献1799条经验 获得超8个赞

返回的文本为空字符串,因此无法转换为 。添加支票float


b = removeprc(a)

if b:

    print(float(b))

else:

    print('b is an empty string')


查看完整回答
反对 回复 2022-08-02
?
紫衣仙女

TA贡献1839条经验 获得超15个赞

您在此处使用了关键字作为变量。这就是为什么有时它不起作用。与 str() 一样,list() 是一种将变量转换为列表的方法。我想,尝试重命名变量,如下所示。


def removeprc(string): #removes the % from a string 

    string = str(string)

    l = string.split('%')

    string = " ".join(l)

    return string


查看完整回答
反对 回复 2022-08-02
  • 3 回答
  • 0 关注
  • 512 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号