I've got a project where I get product data and need to get the prices which come in, in different formats.
Some examples would be: US$17, USD17.00, 17,00€, 17€, GBP17, Only 17,-€, 17.000,00€, 17,000.00$ etc.
So at the beginning I started with one specific string to float function and kept on adding code for specific use cases. I'm sure the code looks horrible and I can see already ways to improve it, but I wanted to get your opinions in the first place.
def convertPriceIntoFloat ( myString ):
myString = myString.strip()
# 1.298,90 €
if "€" in myString and "." in myString and "," in myString:
myString = (myString.replace('€', '')).strip()
myString = (myString.replace('.', '')).strip()
float_price = float(myString.replace(',', '.'))
return(float_price)
if "€" in myString and "*" in myString and "ab" in myString:
myString = (myString.replace('€', '')).strip()
myString = (myString.replace('*', '')).strip()
myString = (myString.replace('ab', '')).strip()
float_price = float(myString.replace(',', '.'))
return(float_price)
if "€" in myString and "ab" in myString:
myString = (myString.replace('€', '')).strip()
myString = (myString.replace('ab', '')).strip()
if re.match('^\d{1,3}\.\d{3}\,\d{2}$', myString) is not None:
# thousand EURO or more
myString = (myString.replace('.', '')).strip()
float_price = float(myString.replace(',', '.'))
else:
float_price = float(myString.replace(',', '.'))
return(float_price)
# 599,- €
if ",-" in myString and "€" in myString:
myString = (myString.replace('€', '')).strip()
myString = (myString.replace(',-', '.00')).strip()
if re.match('^\d{1,3}\.\d{3}\,\d{2}$', myString) is not None:
# thousand EURO or more
myString = (myString.replace('.', '')).strip()
float_price = float(myString.replace(',', '.'))
else:
float_price = float(myString.replace(',', '.'))
return(float_price)
# ↵179,89 €↵*↵
if "€" in myString and "*" in myString:
myString = (myString.replace('€', '')).strip()
myString = (myString.replace('*', '')).strip()
if re.match('^\d{1,3}\.\d{3}\,\d{2}$', myString) is not None:
# thousand EURO or more
myString = (myString.replace('.', '')).strip()
float_price = float(myString.replace(',', '.'))
else:
float_price = float(myString.replace(',', '.'))
return(float_price)
# ab 223,90 EUR
if "EUR" in myString and "ab" in myString:
myString = (myString.replace('EUR', '')).strip()
myString = (myString.replace('ab', '')).strip()
if re.match('^\d{1,3}\.\d{3}\,\d{2}$', myString) is not None:
# thousand EURO or more
myString = (myString.replace('.', '')).strip()
float_price = float(myString.replace(',', '.'))
else:
float_price = float(myString.replace(',', '.'))
return(float_price)
if "EUR" in myString:
# GB Pound
myString = (myString.replace('EUR', '')).strip()
if re.match('^\d{1,3}\.\d{3}\,\d{2}$', myString) is not None:
# thousand EURO or more
myString = (myString.replace('.', '')).strip()
float_price = float(myString.replace(',', '.'))
else:
float_price = float(myString.replace(',', '.'))
return(float_price)
if "CHF" in myString:
# CHF Schweiz
myString = (myString.replace('CHF', '')).strip()
if re.match('^\d{1,3}\.\d{3}\,\d{2}$', myString) is not None:
# thousand Franks or more
myString = (myString.replace('.', '')).strip()
float_price = float(myString.replace(',', '.'))
else:
float_price = float(myString.replace(',', '.'))
return(float_price)
if re.match('^\d{1,3}\.\d{3}\,\d{2}$', myString) is not None:
# thousand EURO or more, coming in as a float already
myString = (myString.replace('.', '')).strip()
float_price = float(myString.replace(',', '.'))
return(float_price)
# 122,60 £
if "£" in myString:
# remove GB Pound sign
myString = (myString.replace('£', '')).strip()
if re.match('^\d{1,3}\.\d{3}\,\d{2}$', myString) is not None:
# thousand GB Pounds or more
myString = (myString.replace('.', '')).strip()
float_price = float(myString.replace(',', '.'))
# 122,60 £
if re.match('^\d{1,3}\,\d{2}$', myString) is not None:
#
myString = (myString.replace('.', '')).strip()
float_price = float(myString.replace(',', '.'))
return(float_price)
if "$" in myString:
# GB Pound
myString = (myString.replace('$', '')).strip()
float_price = float(myString.replace(',', ''))
return(float_price)
if ",-" in myString:
float_price = float(myString.replace(',-', '.00'))
return(float_price)
if re.match('^\d{1,3}\,\d{2}$', myString) is not None:
float_price = float(myString.replace(',', '.'))
return(float_price)
if " " in myString and "€" in myString:
return ( getPriceFromCommaString ( myString ) )
# UVP: 44,95 EURO
if "UVP:" in myString and "EURO" in myString:
myString = (myString.replace('UVP:', '')).strip()
myString = (myString.replace('EURO', '')).strip()
float_price = float(myString.replace(',', '.'))
return(float_price)
# 22,99 €
# € 1.199,99
if "€" in myString:
myString = (myString.replace('€', '')).strip()
if re.match('^\d{1,3}\.\d{3}\,\d{2}$', myString) is not None:
# thousand EURO or more
myString = (myString.replace('.', '')).strip()
float_price = float(myString.replace(',', '.'))
else:
float_price = float(myString.replace(',', '.'))
return(float_price)
else:
return(myString)
If anybody knows a Python library that does the same thing, I'd be happy to flip as well.
1.298,90 €
should the output be1298.9
? \$\endgroup\$