I'm trying to convert a string (such as 10.99) into a float in a for loop, and I can't figure out a way to do it with the data I've scraped from a website. I need to divide the output by another float (also in the for loop). The below is an example of what I'm trying to do
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)
I then get the error:
number_of_units = price / float(price_per_unit)
ValueError: could not convert string to float:
What's the best way to turn price_per_unit into a float, and calculate number_of_units??
Thanks for your help :)
EDIT: Working solution below for anyone else who's interested
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
re.sub()part is not working good, It doesn't return any data. I don't get what were you trying to achieve.