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

我正在用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 loaded

acquiring values ...

values acquired

running eth trade

-0.37

Traceback (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 string


b = float(removeprc(a))

print(b)

它输出-0.06并完美地工作???


所以我真的被困在这里。它应该有效,但它不起作用。更糟糕的是,它有时无缘无故地起作用。当我隔离问题时,它工作正常。


任何帮助将不胜感激!


哦,如果你想看到整个代码,它在这里:https://github.com/Madaxuorel/proj-ethTB/blob/master/ETHtradingbotV1.py


婷婷同学_
浏览 513回答 3
3回答

素胚勾勒不出你

此错误消息...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 WebDriverWaitfrom selenium.webdriver.common.by import Byfrom selenium.webdriver.support import expected_conditions as EC

守着星空守着你

返回的文本为空字符串,因此无法转换为 。添加支票floatb = removeprc(a)if b:&nbsp; &nbsp; print(float(b))else:&nbsp; &nbsp; print('b is an empty string')

紫衣仙女

您在此处使用了关键字作为变量。这就是为什么有时它不起作用。与 str() 一样,list() 是一种将变量转换为列表的方法。我想,尝试重命名变量,如下所示。def removeprc(string): #removes the % from a string&nbsp;&nbsp; &nbsp; string = str(string)&nbsp; &nbsp; l = string.split('%')&nbsp; &nbsp; string = " ".join(l)&nbsp; &nbsp; return string
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python