Function to extract float from different price patterns

Did Darth Vader wear the same suit for 20+ years?

Should I "tell" my exposition or give it through dialogue?

Does the "6 seconds per round" rule apply to speaking/roleplaying during combat situations?

What can plausibly explain many of my very long and low-tech bridges?

Why is c4 bad when playing the London against a King's Indian?

Through what methods and mechanisms can a multi-material FDM printer operate?

Building a road to escape Earth's gravity by making a pyramid on Antartica

Incremental Ranges!

Pay as you go Or Oyster card

Java guess the number

What are the words for people who cause trouble believing they know better?

Pronoun introduced before its antecedent

How to skip replacing first occurrence of a character in each line?

What's the correct term describing the action of sending a brand-new ship out into its first seafaring trip?

On the Twin Paradox Again

How do I write "Show, Don't Tell" as an Asperger?

Do any instruments not produce overtones?

Company did not petition for visa in a timely manner. Is asking me to work from overseas, but wants me to take a paycut

What's the correct term for a waitress in the Middle Ages?

What happens to foam insulation board after you pour concrete slab?

How is TD(0) method helpful? What good does it do?

Are there cubesats in GEO?

What is the purpose of building foundations?

How do photons get into the eyes?



Function to extract float from different price patterns







.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}







5












$begingroup$


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 "&#8364" 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.










share|improve this question









New contributor



Chris is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






$endgroup$












  • $begingroup$
    What does your program actually do?
    $endgroup$
    – Justin
    21 hours ago










  • $begingroup$
    It's a Function to extract float values from price strings. Those strings can have different formats.
    $endgroup$
    – Chris
    21 hours ago










  • $begingroup$
    So for 1.298,90 € should the output be 1298.9?
    $endgroup$
    – Justin
    21 hours ago










  • $begingroup$
    Exactly. And then there are different currencies, different ways of displaying prices etc.
    $endgroup$
    – Chris
    21 hours ago


















5












$begingroup$


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 "&#8364" 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.










share|improve this question









New contributor



Chris is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






$endgroup$












  • $begingroup$
    What does your program actually do?
    $endgroup$
    – Justin
    21 hours ago










  • $begingroup$
    It's a Function to extract float values from price strings. Those strings can have different formats.
    $endgroup$
    – Chris
    21 hours ago










  • $begingroup$
    So for 1.298,90 € should the output be 1298.9?
    $endgroup$
    – Justin
    21 hours ago










  • $begingroup$
    Exactly. And then there are different currencies, different ways of displaying prices etc.
    $endgroup$
    – Chris
    21 hours ago














5












5








5


1



$begingroup$


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 "&#8364" 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.










share|improve this question









New contributor



Chris is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






$endgroup$




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 "&#8364" 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.







python python-3.x parsing floating-point i18n






share|improve this question









New contributor



Chris is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.










share|improve this question









New contributor



Chris is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.








share|improve this question




share|improve this question








edited 2 hours ago









Stephen Rauch

3,75061630




3,75061630






New contributor



Chris is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.








asked 22 hours ago









ChrisChris

1261




1261




New contributor



Chris is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




New contributor




Chris is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.














  • $begingroup$
    What does your program actually do?
    $endgroup$
    – Justin
    21 hours ago










  • $begingroup$
    It's a Function to extract float values from price strings. Those strings can have different formats.
    $endgroup$
    – Chris
    21 hours ago










  • $begingroup$
    So for 1.298,90 € should the output be 1298.9?
    $endgroup$
    – Justin
    21 hours ago










  • $begingroup$
    Exactly. And then there are different currencies, different ways of displaying prices etc.
    $endgroup$
    – Chris
    21 hours ago


















  • $begingroup$
    What does your program actually do?
    $endgroup$
    – Justin
    21 hours ago










  • $begingroup$
    It's a Function to extract float values from price strings. Those strings can have different formats.
    $endgroup$
    – Chris
    21 hours ago










  • $begingroup$
    So for 1.298,90 € should the output be 1298.9?
    $endgroup$
    – Justin
    21 hours ago










  • $begingroup$
    Exactly. And then there are different currencies, different ways of displaying prices etc.
    $endgroup$
    – Chris
    21 hours ago
















$begingroup$
What does your program actually do?
$endgroup$
– Justin
21 hours ago




$begingroup$
What does your program actually do?
$endgroup$
– Justin
21 hours ago












$begingroup$
It's a Function to extract float values from price strings. Those strings can have different formats.
$endgroup$
– Chris
21 hours ago




$begingroup$
It's a Function to extract float values from price strings. Those strings can have different formats.
$endgroup$
– Chris
21 hours ago












$begingroup$
So for 1.298,90 € should the output be 1298.9?
$endgroup$
– Justin
21 hours ago




$begingroup$
So for 1.298,90 € should the output be 1298.9?
$endgroup$
– Justin
21 hours ago












$begingroup$
Exactly. And then there are different currencies, different ways of displaying prices etc.
$endgroup$
– Chris
21 hours ago




$begingroup$
Exactly. And then there are different currencies, different ways of displaying prices etc.
$endgroup$
– Chris
21 hours ago










3 Answers
3






active

oldest

votes


















7












$begingroup$

I agree that this is a little complicated.



I'd recommend you write a set of tests, and let those guide the complexity of the code. The tests would be simple, like,



assertEq convertPriceIntoFloat("1298,90"), 1298.9
assertEq convertPriceIntoFloat("1.298,90"), 1298.9
assertEq convertPriceIntoFloat("1.298,90 €"), 1298.9
...


Then, start out with a simple float conversion in your code, and see if that works, then add test cases and only add code as you need it. If things do seem to be getting overly complicated, refactor... you'll have tests that let you do that easily.



Good luck.






share|improve this answer









$endgroup$





















    4












    $begingroup$



    • From my untrained eye it looks like a simple regex would help ease the problem.



      ^(.*?)([d.,]+)(.*)$


      This is as it results in the following output:



      >>> pprint([re.match('^(.*?)([d.,]+)(.*)$', i).groups() for i in ('US$17', 'USD17.00', '17,00€', '17€', 'GBP17', 'Only 17,-€', '17.000,00€', '17,000.00$')])
      [('US$', '17', ''),
      ('USD', '17.00', ''),
      ('', '17,00', '€'),
      ('', '17', '€'),
      ('GBP', '17', ''),
      ('Only ', '17,', '-€'),
      ('', '17.000,00', '€'),
      ('', '17,000.00', '$')]



    • Now that we have the money all that is left is to convert it to a float.



      Since you have thousands separators then you can't just use float. And so if you pass the 'thousand separator' and the 'decimal place' to the function and use str.translate then you can convert the code into the form you want.




    import re


    def _extract_price(value):
    match = re.match('^(.*?)([d.,]+)(.*)$', value)
    if match is None:
    raise ValueError("Can't extract price")
    return match.groups()


    def _parse_price(price, thousand, decimal):
    trans = str.maketrans(decimal, '.', thousand)
    return float(price.translate(trans))


    def parse_price(value):
    prefix, price, suffix = _extract_price(value)
    if '€' in prefix + suffix:
    thousand = '.'
    decimal = ','
    else:
    thousand = ','
    decimal = '.'
    return _parse_price(price, thousand, decimal)


    >>> [parse_price(i) for i in ('US$17', 'USD17.00', '17,00€', '17€', 'GBP17', 'Only 17,-€', '17.000,00€', '17,000.00$')]
    [17.0, 17.0, 17.0, 17.0, 17.0, 17.0, 17000.0, 17000.0]





    share|improve this answer









    $endgroup$













    • $begingroup$
      I would favor this approach if we have a simple language to deal with. If the examples as presented by the OP represent the full sample space, go for the regex! The more complex the language, the sooner writing your own parser/compiler wins from a regex, no matter how smartly written.
      $endgroup$
      – dfhwze
      9 hours ago










    • $begingroup$
      @dfhwze Yes, regex isn't good with large complex grammar. For simple grammar like the this it's better than writing a parser.
      $endgroup$
      – Peilonrayz
      8 hours ago



















    2












    $begingroup$


    If anybody knows a Python library that does the same thing, I'd be happy to flip as well.




    I suggest you use the "Price and currency parsing utility" -




    Money Parser is a price and currency parsing utility.



    It provides methods to extract price and currency information from the
    raw string.



    There is a lot of different price and currency formats that present
    values with separators, spacing, etc.



    This library may help you to parse such data.




    Here are some examples of what it can do -



    >>> price_str("1.298,90 €")
    '1298.90'

    >>> price_str("599,- €")
    '599'

    >>> price_str("↵179,89 €↵*↵")
    '179.89'

    >>> price_str("ab 223,90 EUR")
    '223.90'

    >>> price_str("122,60 £")
    '122.60'

    >>> price_str("UVP: 44,95 EURO")
    '44.95'

    >>> price_str("22,99 €")
    '22.99'

    >>> price_str(None, default='0')
    '0'

    >>> price_str("€ 1.199,99")
    '1199.99'


    *NOTES - *



    Open Command Prompt and, if you have Python version >= 3.4, then install the Money Parser module using - pip install money-parser.



    Open the Python 3.7 IDLE and call the module - from money_parser import price_str



    Try out an example from above and you'll know that you have achieved your desired results.



    Hope this helps!






    share|improve this answer











    $endgroup$














      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "196"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: false,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: null,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });






      Chris is a new contributor. Be nice, and check out our Code of Conduct.










      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f221456%2ffunction-to-extract-float-from-different-price-patterns%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      7












      $begingroup$

      I agree that this is a little complicated.



      I'd recommend you write a set of tests, and let those guide the complexity of the code. The tests would be simple, like,



      assertEq convertPriceIntoFloat("1298,90"), 1298.9
      assertEq convertPriceIntoFloat("1.298,90"), 1298.9
      assertEq convertPriceIntoFloat("1.298,90 €"), 1298.9
      ...


      Then, start out with a simple float conversion in your code, and see if that works, then add test cases and only add code as you need it. If things do seem to be getting overly complicated, refactor... you'll have tests that let you do that easily.



      Good luck.






      share|improve this answer









      $endgroup$


















        7












        $begingroup$

        I agree that this is a little complicated.



        I'd recommend you write a set of tests, and let those guide the complexity of the code. The tests would be simple, like,



        assertEq convertPriceIntoFloat("1298,90"), 1298.9
        assertEq convertPriceIntoFloat("1.298,90"), 1298.9
        assertEq convertPriceIntoFloat("1.298,90 €"), 1298.9
        ...


        Then, start out with a simple float conversion in your code, and see if that works, then add test cases and only add code as you need it. If things do seem to be getting overly complicated, refactor... you'll have tests that let you do that easily.



        Good luck.






        share|improve this answer









        $endgroup$
















          7












          7








          7





          $begingroup$

          I agree that this is a little complicated.



          I'd recommend you write a set of tests, and let those guide the complexity of the code. The tests would be simple, like,



          assertEq convertPriceIntoFloat("1298,90"), 1298.9
          assertEq convertPriceIntoFloat("1.298,90"), 1298.9
          assertEq convertPriceIntoFloat("1.298,90 €"), 1298.9
          ...


          Then, start out with a simple float conversion in your code, and see if that works, then add test cases and only add code as you need it. If things do seem to be getting overly complicated, refactor... you'll have tests that let you do that easily.



          Good luck.






          share|improve this answer









          $endgroup$



          I agree that this is a little complicated.



          I'd recommend you write a set of tests, and let those guide the complexity of the code. The tests would be simple, like,



          assertEq convertPriceIntoFloat("1298,90"), 1298.9
          assertEq convertPriceIntoFloat("1.298,90"), 1298.9
          assertEq convertPriceIntoFloat("1.298,90 €"), 1298.9
          ...


          Then, start out with a simple float conversion in your code, and see if that works, then add test cases and only add code as you need it. If things do seem to be getting overly complicated, refactor... you'll have tests that let you do that easily.



          Good luck.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered 21 hours ago









          ndpndp

          1,40198




          1,40198

























              4












              $begingroup$



              • From my untrained eye it looks like a simple regex would help ease the problem.



                ^(.*?)([d.,]+)(.*)$


                This is as it results in the following output:



                >>> pprint([re.match('^(.*?)([d.,]+)(.*)$', i).groups() for i in ('US$17', 'USD17.00', '17,00€', '17€', 'GBP17', 'Only 17,-€', '17.000,00€', '17,000.00$')])
                [('US$', '17', ''),
                ('USD', '17.00', ''),
                ('', '17,00', '€'),
                ('', '17', '€'),
                ('GBP', '17', ''),
                ('Only ', '17,', '-€'),
                ('', '17.000,00', '€'),
                ('', '17,000.00', '$')]



              • Now that we have the money all that is left is to convert it to a float.



                Since you have thousands separators then you can't just use float. And so if you pass the 'thousand separator' and the 'decimal place' to the function and use str.translate then you can convert the code into the form you want.




              import re


              def _extract_price(value):
              match = re.match('^(.*?)([d.,]+)(.*)$', value)
              if match is None:
              raise ValueError("Can't extract price")
              return match.groups()


              def _parse_price(price, thousand, decimal):
              trans = str.maketrans(decimal, '.', thousand)
              return float(price.translate(trans))


              def parse_price(value):
              prefix, price, suffix = _extract_price(value)
              if '€' in prefix + suffix:
              thousand = '.'
              decimal = ','
              else:
              thousand = ','
              decimal = '.'
              return _parse_price(price, thousand, decimal)


              >>> [parse_price(i) for i in ('US$17', 'USD17.00', '17,00€', '17€', 'GBP17', 'Only 17,-€', '17.000,00€', '17,000.00$')]
              [17.0, 17.0, 17.0, 17.0, 17.0, 17.0, 17000.0, 17000.0]





              share|improve this answer









              $endgroup$













              • $begingroup$
                I would favor this approach if we have a simple language to deal with. If the examples as presented by the OP represent the full sample space, go for the regex! The more complex the language, the sooner writing your own parser/compiler wins from a regex, no matter how smartly written.
                $endgroup$
                – dfhwze
                9 hours ago










              • $begingroup$
                @dfhwze Yes, regex isn't good with large complex grammar. For simple grammar like the this it's better than writing a parser.
                $endgroup$
                – Peilonrayz
                8 hours ago
















              4












              $begingroup$



              • From my untrained eye it looks like a simple regex would help ease the problem.



                ^(.*?)([d.,]+)(.*)$


                This is as it results in the following output:



                >>> pprint([re.match('^(.*?)([d.,]+)(.*)$', i).groups() for i in ('US$17', 'USD17.00', '17,00€', '17€', 'GBP17', 'Only 17,-€', '17.000,00€', '17,000.00$')])
                [('US$', '17', ''),
                ('USD', '17.00', ''),
                ('', '17,00', '€'),
                ('', '17', '€'),
                ('GBP', '17', ''),
                ('Only ', '17,', '-€'),
                ('', '17.000,00', '€'),
                ('', '17,000.00', '$')]



              • Now that we have the money all that is left is to convert it to a float.



                Since you have thousands separators then you can't just use float. And so if you pass the 'thousand separator' and the 'decimal place' to the function and use str.translate then you can convert the code into the form you want.




              import re


              def _extract_price(value):
              match = re.match('^(.*?)([d.,]+)(.*)$', value)
              if match is None:
              raise ValueError("Can't extract price")
              return match.groups()


              def _parse_price(price, thousand, decimal):
              trans = str.maketrans(decimal, '.', thousand)
              return float(price.translate(trans))


              def parse_price(value):
              prefix, price, suffix = _extract_price(value)
              if '€' in prefix + suffix:
              thousand = '.'
              decimal = ','
              else:
              thousand = ','
              decimal = '.'
              return _parse_price(price, thousand, decimal)


              >>> [parse_price(i) for i in ('US$17', 'USD17.00', '17,00€', '17€', 'GBP17', 'Only 17,-€', '17.000,00€', '17,000.00$')]
              [17.0, 17.0, 17.0, 17.0, 17.0, 17.0, 17000.0, 17000.0]





              share|improve this answer









              $endgroup$













              • $begingroup$
                I would favor this approach if we have a simple language to deal with. If the examples as presented by the OP represent the full sample space, go for the regex! The more complex the language, the sooner writing your own parser/compiler wins from a regex, no matter how smartly written.
                $endgroup$
                – dfhwze
                9 hours ago










              • $begingroup$
                @dfhwze Yes, regex isn't good with large complex grammar. For simple grammar like the this it's better than writing a parser.
                $endgroup$
                – Peilonrayz
                8 hours ago














              4












              4








              4





              $begingroup$



              • From my untrained eye it looks like a simple regex would help ease the problem.



                ^(.*?)([d.,]+)(.*)$


                This is as it results in the following output:



                >>> pprint([re.match('^(.*?)([d.,]+)(.*)$', i).groups() for i in ('US$17', 'USD17.00', '17,00€', '17€', 'GBP17', 'Only 17,-€', '17.000,00€', '17,000.00$')])
                [('US$', '17', ''),
                ('USD', '17.00', ''),
                ('', '17,00', '€'),
                ('', '17', '€'),
                ('GBP', '17', ''),
                ('Only ', '17,', '-€'),
                ('', '17.000,00', '€'),
                ('', '17,000.00', '$')]



              • Now that we have the money all that is left is to convert it to a float.



                Since you have thousands separators then you can't just use float. And so if you pass the 'thousand separator' and the 'decimal place' to the function and use str.translate then you can convert the code into the form you want.




              import re


              def _extract_price(value):
              match = re.match('^(.*?)([d.,]+)(.*)$', value)
              if match is None:
              raise ValueError("Can't extract price")
              return match.groups()


              def _parse_price(price, thousand, decimal):
              trans = str.maketrans(decimal, '.', thousand)
              return float(price.translate(trans))


              def parse_price(value):
              prefix, price, suffix = _extract_price(value)
              if '€' in prefix + suffix:
              thousand = '.'
              decimal = ','
              else:
              thousand = ','
              decimal = '.'
              return _parse_price(price, thousand, decimal)


              >>> [parse_price(i) for i in ('US$17', 'USD17.00', '17,00€', '17€', 'GBP17', 'Only 17,-€', '17.000,00€', '17,000.00$')]
              [17.0, 17.0, 17.0, 17.0, 17.0, 17.0, 17000.0, 17000.0]





              share|improve this answer









              $endgroup$





              • From my untrained eye it looks like a simple regex would help ease the problem.



                ^(.*?)([d.,]+)(.*)$


                This is as it results in the following output:



                >>> pprint([re.match('^(.*?)([d.,]+)(.*)$', i).groups() for i in ('US$17', 'USD17.00', '17,00€', '17€', 'GBP17', 'Only 17,-€', '17.000,00€', '17,000.00$')])
                [('US$', '17', ''),
                ('USD', '17.00', ''),
                ('', '17,00', '€'),
                ('', '17', '€'),
                ('GBP', '17', ''),
                ('Only ', '17,', '-€'),
                ('', '17.000,00', '€'),
                ('', '17,000.00', '$')]



              • Now that we have the money all that is left is to convert it to a float.



                Since you have thousands separators then you can't just use float. And so if you pass the 'thousand separator' and the 'decimal place' to the function and use str.translate then you can convert the code into the form you want.




              import re


              def _extract_price(value):
              match = re.match('^(.*?)([d.,]+)(.*)$', value)
              if match is None:
              raise ValueError("Can't extract price")
              return match.groups()


              def _parse_price(price, thousand, decimal):
              trans = str.maketrans(decimal, '.', thousand)
              return float(price.translate(trans))


              def parse_price(value):
              prefix, price, suffix = _extract_price(value)
              if '€' in prefix + suffix:
              thousand = '.'
              decimal = ','
              else:
              thousand = ','
              decimal = '.'
              return _parse_price(price, thousand, decimal)


              >>> [parse_price(i) for i in ('US$17', 'USD17.00', '17,00€', '17€', 'GBP17', 'Only 17,-€', '17.000,00€', '17,000.00$')]
              [17.0, 17.0, 17.0, 17.0, 17.0, 17.0, 17000.0, 17000.0]






              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered 9 hours ago









              PeilonrayzPeilonrayz

              27.8k343114




              27.8k343114












              • $begingroup$
                I would favor this approach if we have a simple language to deal with. If the examples as presented by the OP represent the full sample space, go for the regex! The more complex the language, the sooner writing your own parser/compiler wins from a regex, no matter how smartly written.
                $endgroup$
                – dfhwze
                9 hours ago










              • $begingroup$
                @dfhwze Yes, regex isn't good with large complex grammar. For simple grammar like the this it's better than writing a parser.
                $endgroup$
                – Peilonrayz
                8 hours ago


















              • $begingroup$
                I would favor this approach if we have a simple language to deal with. If the examples as presented by the OP represent the full sample space, go for the regex! The more complex the language, the sooner writing your own parser/compiler wins from a regex, no matter how smartly written.
                $endgroup$
                – dfhwze
                9 hours ago










              • $begingroup$
                @dfhwze Yes, regex isn't good with large complex grammar. For simple grammar like the this it's better than writing a parser.
                $endgroup$
                – Peilonrayz
                8 hours ago
















              $begingroup$
              I would favor this approach if we have a simple language to deal with. If the examples as presented by the OP represent the full sample space, go for the regex! The more complex the language, the sooner writing your own parser/compiler wins from a regex, no matter how smartly written.
              $endgroup$
              – dfhwze
              9 hours ago




              $begingroup$
              I would favor this approach if we have a simple language to deal with. If the examples as presented by the OP represent the full sample space, go for the regex! The more complex the language, the sooner writing your own parser/compiler wins from a regex, no matter how smartly written.
              $endgroup$
              – dfhwze
              9 hours ago












              $begingroup$
              @dfhwze Yes, regex isn't good with large complex grammar. For simple grammar like the this it's better than writing a parser.
              $endgroup$
              – Peilonrayz
              8 hours ago




              $begingroup$
              @dfhwze Yes, regex isn't good with large complex grammar. For simple grammar like the this it's better than writing a parser.
              $endgroup$
              – Peilonrayz
              8 hours ago











              2












              $begingroup$


              If anybody knows a Python library that does the same thing, I'd be happy to flip as well.




              I suggest you use the "Price and currency parsing utility" -




              Money Parser is a price and currency parsing utility.



              It provides methods to extract price and currency information from the
              raw string.



              There is a lot of different price and currency formats that present
              values with separators, spacing, etc.



              This library may help you to parse such data.




              Here are some examples of what it can do -



              >>> price_str("1.298,90 €")
              '1298.90'

              >>> price_str("599,- €")
              '599'

              >>> price_str("↵179,89 €↵*↵")
              '179.89'

              >>> price_str("ab 223,90 EUR")
              '223.90'

              >>> price_str("122,60 £")
              '122.60'

              >>> price_str("UVP: 44,95 EURO")
              '44.95'

              >>> price_str("22,99 €")
              '22.99'

              >>> price_str(None, default='0')
              '0'

              >>> price_str("€ 1.199,99")
              '1199.99'


              *NOTES - *



              Open Command Prompt and, if you have Python version >= 3.4, then install the Money Parser module using - pip install money-parser.



              Open the Python 3.7 IDLE and call the module - from money_parser import price_str



              Try out an example from above and you'll know that you have achieved your desired results.



              Hope this helps!






              share|improve this answer











              $endgroup$


















                2












                $begingroup$


                If anybody knows a Python library that does the same thing, I'd be happy to flip as well.




                I suggest you use the "Price and currency parsing utility" -




                Money Parser is a price and currency parsing utility.



                It provides methods to extract price and currency information from the
                raw string.



                There is a lot of different price and currency formats that present
                values with separators, spacing, etc.



                This library may help you to parse such data.




                Here are some examples of what it can do -



                >>> price_str("1.298,90 €")
                '1298.90'

                >>> price_str("599,- €")
                '599'

                >>> price_str("↵179,89 €↵*↵")
                '179.89'

                >>> price_str("ab 223,90 EUR")
                '223.90'

                >>> price_str("122,60 £")
                '122.60'

                >>> price_str("UVP: 44,95 EURO")
                '44.95'

                >>> price_str("22,99 €")
                '22.99'

                >>> price_str(None, default='0')
                '0'

                >>> price_str("€ 1.199,99")
                '1199.99'


                *NOTES - *



                Open Command Prompt and, if you have Python version >= 3.4, then install the Money Parser module using - pip install money-parser.



                Open the Python 3.7 IDLE and call the module - from money_parser import price_str



                Try out an example from above and you'll know that you have achieved your desired results.



                Hope this helps!






                share|improve this answer











                $endgroup$
















                  2












                  2








                  2





                  $begingroup$


                  If anybody knows a Python library that does the same thing, I'd be happy to flip as well.




                  I suggest you use the "Price and currency parsing utility" -




                  Money Parser is a price and currency parsing utility.



                  It provides methods to extract price and currency information from the
                  raw string.



                  There is a lot of different price and currency formats that present
                  values with separators, spacing, etc.



                  This library may help you to parse such data.




                  Here are some examples of what it can do -



                  >>> price_str("1.298,90 €")
                  '1298.90'

                  >>> price_str("599,- €")
                  '599'

                  >>> price_str("↵179,89 €↵*↵")
                  '179.89'

                  >>> price_str("ab 223,90 EUR")
                  '223.90'

                  >>> price_str("122,60 £")
                  '122.60'

                  >>> price_str("UVP: 44,95 EURO")
                  '44.95'

                  >>> price_str("22,99 €")
                  '22.99'

                  >>> price_str(None, default='0')
                  '0'

                  >>> price_str("€ 1.199,99")
                  '1199.99'


                  *NOTES - *



                  Open Command Prompt and, if you have Python version >= 3.4, then install the Money Parser module using - pip install money-parser.



                  Open the Python 3.7 IDLE and call the module - from money_parser import price_str



                  Try out an example from above and you'll know that you have achieved your desired results.



                  Hope this helps!






                  share|improve this answer











                  $endgroup$




                  If anybody knows a Python library that does the same thing, I'd be happy to flip as well.




                  I suggest you use the "Price and currency parsing utility" -




                  Money Parser is a price and currency parsing utility.



                  It provides methods to extract price and currency information from the
                  raw string.



                  There is a lot of different price and currency formats that present
                  values with separators, spacing, etc.



                  This library may help you to parse such data.




                  Here are some examples of what it can do -



                  >>> price_str("1.298,90 €")
                  '1298.90'

                  >>> price_str("599,- €")
                  '599'

                  >>> price_str("↵179,89 €↵*↵")
                  '179.89'

                  >>> price_str("ab 223,90 EUR")
                  '223.90'

                  >>> price_str("122,60 £")
                  '122.60'

                  >>> price_str("UVP: 44,95 EURO")
                  '44.95'

                  >>> price_str("22,99 €")
                  '22.99'

                  >>> price_str(None, default='0')
                  '0'

                  >>> price_str("€ 1.199,99")
                  '1199.99'


                  *NOTES - *



                  Open Command Prompt and, if you have Python version >= 3.4, then install the Money Parser module using - pip install money-parser.



                  Open the Python 3.7 IDLE and call the module - from money_parser import price_str



                  Try out an example from above and you'll know that you have achieved your desired results.



                  Hope this helps!







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited 17 hours ago

























                  answered 19 hours ago









                  JustinJustin

                  1,123327




                  1,123327






















                      Chris is a new contributor. Be nice, and check out our Code of Conduct.










                      draft saved

                      draft discarded


















                      Chris is a new contributor. Be nice, and check out our Code of Conduct.













                      Chris is a new contributor. Be nice, and check out our Code of Conduct.












                      Chris is a new contributor. Be nice, and check out our Code of Conduct.
















                      Thanks for contributing an answer to Code Review Stack Exchange!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      Use MathJax to format equations. MathJax reference.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f221456%2ffunction-to-extract-float-from-different-price-patterns%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Taj Mahal Inhaltsverzeichnis Aufbau | Geschichte | 350-Jahr-Feier | Heutige Bedeutung | Siehe auch |...

                      Baia Sprie Cuprins Etimologie | Istorie | Demografie | Politică și administrație | Arii naturale...

                      Nicolae Petrescu-Găină Cuprins Biografie | Opera | In memoriam | Varia | Controverse, incertitudini...