Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/365.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python ValueError:无法将字符串转换为浮点:';31950';_Python_String_Floating Point_Valueerror - Fatal编程技术网

Python ValueError:无法将字符串转换为浮点:';31950';

Python ValueError:无法将字符串转换为浮点:';31950';,python,string,floating-point,valueerror,Python,String,Floating Point,Valueerror,这是我的代码: def check_price(): page = requests.get(URL, headers=headers) soup = BeautifulSoup(page.content, 'html.parser') title = soup.find(id="itemTitle").get_text() price = soup.find(id="prcIsum").get_text() converted_price = fl

这是我的代码:

def check_price():
    page = requests.get(URL, headers=headers)

    soup = BeautifulSoup(page.content, 'html.parser')


    title = soup.find(id="itemTitle").get_text()
    price = soup.find(id="prcIsum").get_text()
    converted_price = float(price[4:10])
    converted_title = title[16:61]

    if(converted_price < 31.950):
        send_mail()

    print(converted_title)
    print(converted_price)

    if(converted_price > 31.000):
        send_mail()
def check_price():
page=requests.get(URL,headers=headers)
soup=BeautifulSoup(page.content,'html.parser')
title=soup.find(id=“itemTitle”).get_text()
price=soup.find(id=“prcIsum”).get_text()
转换价格=浮动(价格[4:10])
转换后的标题=标题[16:61]
如果(换算价格<31.950):
发送邮件
打印(转换为标题)
打印(换算价格)
如果(换算价格>31.000):
发送邮件
这就是我犯的错误

  File "C:/Users/Mario/PycharmProjects/Bitanga/AI.py", line 55, in <module>
    check_price()
  File "C:/Users/Mario/PycharmProjects/Bitanga/AI.py", line 20, in check_price
    converted_price = float(price[4:10])
  ValueError: could not convert string to float: '31,950'
文件“C:/Users/Mario/PycharmProjects/Bitanga/AI.py”,第55行,在
核对价格
文件“C:/Users/Mario/PycharmProjects/Bitanga/AI.py”,第20行,检查价格
转换价格=浮动(价格[4:10])
ValueError:无法将字符串转换为浮点:“31950”

您必须将字符串
31950
中的
替换为
,以便python可以将其识别为十进制分隔符。使用:

soup=BeautifulSoup(page.content,'html.parser')
title=soup.find(id=“itemTitle”).get_text()
price=soup.find(id=“prcIsum”).get_text()
转换后的价格=浮动(价格[4:10]。替换(',','))
转换后的标题=标题[16:61]
如果(换算价格<31.950):
发送邮件
打印(转换为标题)
打印(换算价格)
如果(换算价格>31.000):
发送邮件

您必须将字符串
31950
中的
替换为
,以便python可以将其识别为十进制分隔符。使用:

soup=BeautifulSoup(page.content,'html.parser')
title=soup.find(id=“itemTitle”).get_text()
price=soup.find(id=“prcIsum”).get_text()
转换后的价格=浮动(价格[4:10]。替换(',','))
转换后的标题=标题[16:61]
如果(换算价格<31.950):
发送邮件
打印(转换为标题)
打印(换算价格)
如果(换算价格>31.000):
发送邮件

这是因为您的字符串包含无法转换的字符“,”。 对于从字符串到int、float、double、long的转换,字符串不能有任何看起来不像数字的字符。 或者可以使用正则表达式删除所有此类字符,以便字符串中只有纯数字。 例如


number=str[/\d+(?:.\d+?/])

这是因为您的字符串包含无法转换的字符“,”。 对于从字符串到int、float、double、long的转换,字符串不能有任何看起来不像数字的字符。 或者可以使用正则表达式删除所有此类字符,以便字符串中只有纯数字。 例如


number=str[/\d+(?:.\d+?/])

根据,Python仅识别带有
的浮点数作为十进制分隔符。如果要将字符串
'31950'
解析为浮点数
31.95
,则需要替换逗号。如果它应该是
31950.0
,则需要删除逗号。“31950”不是有效的浮点数据。请将
替换为
以将字符串转换为浮点。您从该错误消息中了解到了什么?Python仅根据将带有
的浮点识别为十进制分隔符。如果要将字符串
'31950'
解析为浮点数
31.95
,则需要替换逗号。如果它应该是
31950.0
,您需要删除逗号。“31950”不是有效的浮点数据。请将
替换为
以将字符串转换为浮点。您从错误消息中了解到了什么?谢谢!我名声不好,所以我不能投你一票。。但是非常感谢!如果一个答案解决了你的问题,那么它会很有帮助。谢谢!我名声不好,所以我不能投你一票。。但是非常感谢!如果一个答案已经解决了你的问题,那么它会很有帮助的。