value error string can not be converted to float?
Question:
import scrapy
from scrapy import Field,Item
from itemloaders.processors import TakeFirst,MapCompose
def get_price(txt):
value = txt.replace('1.359,20','1.359')
value = txt.replace(',','.')
value = txt.strip('1.499,00')
value = txt.strip('R$')
float(value)
class MultilaserItem(scrapy.Item):
# define the fields for your item here like:
# name = scrapy.Field()
title = Field(output_process=TakeFirst())
price = Field(input_processor=MapCompose(get_price),output_processor=TakeFirst())
#installment = Field(output_process=TakeFirst())
your text when i do this value = txt.replace(',','.')
should it not replace the value in 14,90
to 14.90
? but i am stuck with error
ValueError: Error with input processor MapCompose: field='price' value=['R$ 14,90'] error='ValueError: Error in MapCompose with <function get_price at 0x7fa825ee0b80> value=['R$ 14,90'] error='ValueError: could not convert string to float: ' 14,90
Answers:
The posted code above only applies the last operation value = txt.strip('R$')
that results with " 14,90" which is not a valid floating point number and raises a value error.
To perform multiple operations on a string then need to reassign the variable after each operation.
Try a function like the following to get the price from a string input:
def get_price(txt):
txt = txt.replace(',','.')
value = txt.strip('R$')
return float(value)
price = get_price("R$ 14,90")
print(f"{price:.2f}")
Output:
14.90
value = txt.replace('1.359,20','1.359')
value = txt.replace(',','.')
value = txt.strip('1.499,00')
value = txt.strip('R$')
These replacements use the original value of txt
, so they are not cumulative. Each one undoes the previous one. So you end up with only the final replacement of strip('R$')
. The first three are discarded.
import scrapy
from scrapy import Field,Item
from itemloaders.processors import TakeFirst,MapCompose
def get_price(txt):
value = txt.replace('1.359,20','1.359')
value = txt.replace(',','.')
value = txt.strip('1.499,00')
value = txt.strip('R$')
float(value)
class MultilaserItem(scrapy.Item):
# define the fields for your item here like:
# name = scrapy.Field()
title = Field(output_process=TakeFirst())
price = Field(input_processor=MapCompose(get_price),output_processor=TakeFirst())
#installment = Field(output_process=TakeFirst())
your text when i do this value = txt.replace(',','.')
should it not replace the value in 14,90
to 14.90
? but i am stuck with error
ValueError: Error with input processor MapCompose: field='price' value=['R$ 14,90'] error='ValueError: Error in MapCompose with <function get_price at 0x7fa825ee0b80> value=['R$ 14,90'] error='ValueError: could not convert string to float: ' 14,90
The posted code above only applies the last operation value = txt.strip('R$')
that results with " 14,90" which is not a valid floating point number and raises a value error.
To perform multiple operations on a string then need to reassign the variable after each operation.
Try a function like the following to get the price from a string input:
def get_price(txt):
txt = txt.replace(',','.')
value = txt.strip('R$')
return float(value)
price = get_price("R$ 14,90")
print(f"{price:.2f}")
Output:
14.90
value = txt.replace('1.359,20','1.359')
value = txt.replace(',','.')
value = txt.strip('1.499,00')
value = txt.strip('R$')
These replacements use the original value of txt
, so they are not cumulative. Each one undoes the previous one. So you end up with only the final replacement of strip('R$')
. The first three are discarded.