无法在for循环中将字符串转换为float

时间:2019-12-04 16:32:28

标签: python for-loop

我正在尝试在for循环中将字符串(例如10.99)转换为浮点数,但我想不出一种方法来处理从网站上抓取的数据。我需要将输出除以另一个浮点数(也在for循环中)。下面是我正在尝试做的一个例子

import re

test_data = ['\n\t\t\t\t£10.00 per 100ML', '\xa0', '\n\t\t\t\t£0.40 per EACH', '\xa0', '\xa0', '\xa0', '\xa0', '\n\t\t\t\t£0.54 per EACH', '\n\t\t\t\t£1.33 per EACH']
price_data = [100, 10.99, 20.99, 25.25, 30, 35, 40, 54, 3]

for items in zip(test_data, price_data):
    characters = re.sub("\[p].*$|[^\d\.]", "", items[0])
    price_per_unit = characters[0:5]

    price = items[1]

    number_of_units = price / float(price_per_unit)

然后我得到了错误:

    number_of_units = price / float(price_per_unit)
ValueError: could not convert string to float: 

将price_per_unit转换为浮动货币并计算number_of_units的最佳方法是什么?

感谢您的帮助:)

编辑:下面对其他感兴趣的人有用的解决方案

import re

test_data = ['\n\t\t\t\t£10.00 per 100ML', '\xa0', '\n\t\t\t\t£0.40 per EACH', '\xa0', '\xa0', '\xa0', '\xa0', '\n\t\t\t\t£0.54 per EACH', '\n\t\t\t\t£1.33 per EACH']
price_data = [100, 10.99, 20.99, 25.25, 30, 35, 40, 54, 3]

for items in zip(test_data, price_data):
    price = items[1]

    characters = re.sub("\[p].*$|[^\d\.]", "", items[0])
    price_per_unit = characters[0:5]
    if price_per_unit.replace('.', '', 1).isdigit():
        price_per_unit_formatted = float(price_per_unit)
        number_of_units = price / price_per_unit_formatted
    else:
        price_per_unit = None
        number_of_units = None

5 个答案:

答案 0 :(得分:3)

您的问题不是来自float()函数。当代码解析test_data时,'\xa0'将返回一个空字符串'',该空字符串无法转换为浮点值。

希望这会有所帮助。

答案 1 :(得分:1)

正如 Bill 所说,您的问题是由于某些price_per_unit''而引起的。 解决此问题的一个简单方法是,以下列方式确保price_per_unit确实是一个数字:

if price_per_unit.replace('.','',1).isdigit():
      number_of_units = price / float(price_per_unit)

这将忽略那些''并仍然保持其功能

答案 2 :(得分:1)

如果我了解您的要求:

number_of_units=[]
for items in zip(test_data, price_data):
  if (items[0]!='\xa0'):
    characters = re.sub("\[p].*$|[^\d\.]", "", items[0])
    price_per_unit = characters[0:5]

    price = items[1]

    number_of_units.append(price / float(price_per_unit))
  else:
    number_of_units.append(1)

number_of_units #[10.0, 1, 52.474999999999994, 1, 1, 1, 1, 100.0, 2.255639097744361]

将'\ xa0'元素视为1个不可分割的单元。

使用列表存储循环中生成的所有值,而使用代码将仅存储最后一个。

答案 3 :(得分:0)

\ xa0变成一个空字符串,您应该处理它

import re

test_data = ['\n\t\t\t\t£10.00 per 100ML', '\xa0', '\n\t\t\t\t£0.40 per EACH', '\xa0', '\xa0', '\xa0', '\xa0', '\n\t\t\t\t£0.54 per EACH', '\n\t\t\t\t£1.33 per EACH']
price_data = [100, 10.99, 20.99, 25.25, 30, 35, 40, 54, 3]

for items in zip(test_data, price_data):

    characters = re.sub("\[p].*$|[^\d\.]", "", items[0])

    price_per_unit = characters[:5]
    if price_per_unit == '':
      print('empty')
      break
    price = items[1]

    number_of_units = price/ float(price_per_unit)
    print(number_of_units)

答案 4 :(得分:0)

当您执行Test_Data的SUBSTR时,存在像'\ xa0'这样的字符串,那么它将在price_per_unit变量中提供一个空字符串。为避免这种情况,您可以将其替换为“ 1”,因为用“ 0”替换将产生除以零的错误。

    import re

test_data = ['\n\t\t\t\t£10.00 per 100ML', '\xa0', '\n\t\t\t\t£0.40 per EACH', '\xa0', '\xa0', '\xa0', '\xa0', '\n\t\t\t\t£0.54 per EACH', '\n\t\t\t\t£1.33 per EACH']
price_data = [100, 10.99, 20.99, 25.25, 30, 35, 40, 54, 3]

for items in zip(test_data, price_data):
    characters = re.sub("\[p].*$|[^\d\.]", "", items[0])
    price_per_unit = characters[0:5]


    price = items[1]

    if price_per_unit == '':
      price_per_unit = '1'
    else:
      price_per_unit

    print('---------')
    number_of_units = price / float(price_per_unit)
    print(number_of_units)

为了更好地理解错误,最好在出现错误时打印变量。这样我才知道为什么发生此问题。