ValueError:无法在python 3中将字符串转换为float:'' [英] ValueError: could not convert string to float: '' in python 3

查看:80
本文介绍了ValueError:无法在python 3中将字符串转换为float:''的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我正在用python编写一个小机器人,但遇到了问题.这似乎是一个普遍的问题,但是我从未在遇到过同样的情况下问过它.

So I'm coding a little bot in python and I'm having a problem. It seems to be a common problem, but I've never seen it asked in the same situation I'm in.

好的,这是代码构成问题:

Ok so here is the code posing problem :

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"是我创建的用于删除数字%的小功能,它是:

browser.find_element_by_xpath('//*[@id="draggableNavRightResizable"]/section/section[2]/section[1]/div[3]/ul/li[1]/div[2]/div[6]/span').text) is a selenium tool used to get a value of a website. as you will see later, the element retrieved is a number wich should work with float() "remove prc" is a little function I created to remove the % of a number, here it is :

def removeprc(string): #removes the % from a string 
    string = str(string)
    list = string.split('%')
    string = " ".join(list)

    return string

这可能不是最好的方法,但是当我单独测试时,它就可以工作.

It's probably not the best way to do it, but it works when I test it alone.

无论如何,这是我运行全部代码后得到的结果

anyway, here is what I get when I run my entire code

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行是无用的.在第六行,我打印了我要获取的float()的.如您所见,它应该工作,并且……可以!有时.

the first 5 lines are useless. on the 6th line, I printed what i am trying to get the float() of. As you can see, it should work and ... It does ! sometimes.

这是最不可思议的事情,它完美地运行了一半时间!我在互联网上已经读到,如果您尝试将非数字或奇怪的东西(例如空格)浮动(),则可能会发生这种情况.如您所见,我认为情况并非如此.

that's the weirdest thing about this, it works perfectly half the time! I've read on the internet that this can happen if you try to float() things that are not numbers or that have weird shit in them, like spaces. As you can see, i think it's not the case here.

当我尝试通过运行程序的简化版来找出问题所在时:

When I try to isolate the problem by running a simplified version of the program like this :

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并完美运行???

it outputs -0.06 and works perfectly ???

所以我真的被困在这里.它应该可以,但是不能.更糟糕的是,它有时会有时正常工作.当我找出问题所在时,它工作正常.

So I'm really stuck here. It should work, but it doesn't. Even worst, it works sometimes, for no reason. And when I isolate the problem, it works fine.

任何帮助将不胜感激!

哦,如果您想查看整个代码,则在这里:

Oh and if you want to see the entire code, it's here: https://github.com/Madaxuorel/proj-ethTB/blob/master/ETHtradingbotV1.py

推荐答案

此错误消息...

ValueError: could not convert string to float: ''

...表示 Python 解释程序无法将字符串转换为float.

...implies that the Python interpreter was unable to convert a string to float.

你已经足够亲密了. text 方法将返回一个 string 并去除,而不是 string.split('%')您要 list = string.split('%')[0] .

You were close enough. text method would return a string and to strip off the %, instead of string.split('%') you want list = string.split('%')[0].

一个例子:

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

打印:

99

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

Further, find_element_by_xpath() will identify only a single element, and using text you would get a single string, so string = " ".join(list) seems redundant.

如此有效地,要剥离,将 string 转换为 float 并打印,您的有效代码行将是:

So effectively, to strip the %, convert the string to float and print, your effective line of code will be:

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 中呈现带有所需文本的元素.作为解决方案,您需要为 visibility_of_element_located()引入 WebDriverWait ,并且可以使用以下


Update

You are still seeing the error as the element with the required text haven't rendered within the DOM when the line of code is invoked. As a solution you need to induce WebDriverWait for the visibility_of_element_located() and you can use the following Locator Strategy:

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]))

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

Note : You have to add the following imports :

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

这篇关于ValueError:无法在python 3中将字符串转换为float:''的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆