Function to extract float from different price patterns

Predict the product from the reaction

Why is Havana covered in 5-digit numbers in Our Man in Havana?

Is the author of the Shu"t HaRidvaz the same one as the one known to be the rebbe of the Ariza"l?

Can the pre-order traversal of two different trees be the same even though they are different?

Implementation of the Jacobi Symbol in C

Does Snape have a bad hairstyle according to the wizarding world?

Definition of 'vrit'

I calculated that we should be able to see the sun well beyond the observable universe. Where did I go wrong?

In a list with unique pairs A, B, how can I sort them so that the last B is the first A in the next pair?

reverse a call to mmap()

Leaving job close to major deadlines

Synaptic Static - when to roll the d6?

Are there any individual aliens that have gained superpowers in the Marvel universe?

Are intrusions within a foreign embassy considered an act of war?

How Hebrew Vowels Work

How do I find which software is doing an SSH connection?

Would a 7805 5 V regulator drain a 9 V battery?

Why is it 出差去 and not 去出差?

Why was New Asgard established at this place?

Is there any way to revive my Sim?

How "fast" do astronomical events occur?

Counterfeit checks were created for my account. How does this type of fraud work?

What kind of chart is this?

Is declining an undergraduate award which causes me discomfort appropriate?



Function to extract float from different price patterns







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








6












$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('^d1,3.d3,d2$', 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('^d1,3.d3,d2$', 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('^d1,3.d3,d2$', 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('^d1,3.d3,d2$', 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('^d1,3.d3,d2$', 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('^d1,3.d3,d2$', 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('^d1,3.d3,d2$', 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('^d1,3.d3,d2$', myString) is not None:
# thousand GB Pounds or more
myString = (myString.replace('.', '')).strip()
float_price = float(myString.replace(',', '.'))
# 122,60 £
if re.match('^d1,3,d2$', 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('^d1,3,d2$', 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('^d1,3.d3,d2$', 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











$endgroup$











  • $begingroup$
    What does your program actually do?
    $endgroup$
    – Justin
    Jun 1 at 4:35










  • $begingroup$
    It's a Function to extract float values from price strings. Those strings can have different formats.
    $endgroup$
    – Chris
    Jun 1 at 4:40










  • $begingroup$
    So for 1.298,90 € should the output be 1298.9?
    $endgroup$
    – Justin
    Jun 1 at 4:47










  • $begingroup$
    Exactly. And then there are different currencies, different ways of displaying prices etc.
    $endgroup$
    – Chris
    Jun 1 at 5:22










  • $begingroup$
    I don't have experience with Python, but if I understood your problem correctly, isn't it the easiest way to just extract those characters that are a number and parse them as a float? Either with a simple regex or string functions.
    $endgroup$
    – Charmander
    Jun 2 at 11:52

















6












$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('^d1,3.d3,d2$', 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('^d1,3.d3,d2$', 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('^d1,3.d3,d2$', 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('^d1,3.d3,d2$', 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('^d1,3.d3,d2$', 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('^d1,3.d3,d2$', 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('^d1,3.d3,d2$', 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('^d1,3.d3,d2$', myString) is not None:
# thousand GB Pounds or more
myString = (myString.replace('.', '')).strip()
float_price = float(myString.replace(',', '.'))
# 122,60 £
if re.match('^d1,3,d2$', 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('^d1,3,d2$', 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('^d1,3.d3,d2$', 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











$endgroup$











  • $begingroup$
    What does your program actually do?
    $endgroup$
    – Justin
    Jun 1 at 4:35










  • $begingroup$
    It's a Function to extract float values from price strings. Those strings can have different formats.
    $endgroup$
    – Chris
    Jun 1 at 4:40










  • $begingroup$
    So for 1.298,90 € should the output be 1298.9?
    $endgroup$
    – Justin
    Jun 1 at 4:47










  • $begingroup$
    Exactly. And then there are different currencies, different ways of displaying prices etc.
    $endgroup$
    – Chris
    Jun 1 at 5:22










  • $begingroup$
    I don't have experience with Python, but if I understood your problem correctly, isn't it the easiest way to just extract those characters that are a number and parse them as a float? Either with a simple regex or string functions.
    $endgroup$
    – Charmander
    Jun 2 at 11:52













6












6








6


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('^d1,3.d3,d2$', 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('^d1,3.d3,d2$', 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('^d1,3.d3,d2$', 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('^d1,3.d3,d2$', 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('^d1,3.d3,d2$', 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('^d1,3.d3,d2$', 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('^d1,3.d3,d2$', 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('^d1,3.d3,d2$', myString) is not None:
# thousand GB Pounds or more
myString = (myString.replace('.', '')).strip()
float_price = float(myString.replace(',', '.'))
# 122,60 £
if re.match('^d1,3,d2$', 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('^d1,3,d2$', 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('^d1,3.d3,d2$', 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











$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('^d1,3.d3,d2$', 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('^d1,3.d3,d2$', 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('^d1,3.d3,d2$', 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('^d1,3.d3,d2$', 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('^d1,3.d3,d2$', 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('^d1,3.d3,d2$', 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('^d1,3.d3,d2$', 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('^d1,3.d3,d2$', myString) is not None:
# thousand GB Pounds or more
myString = (myString.replace('.', '')).strip()
float_price = float(myString.replace(',', '.'))
# 122,60 £
if re.match('^d1,3,d2$', 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('^d1,3,d2$', 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('^d1,3.d3,d2$', 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















share|improve this question













share|improve this question




share|improve this question








edited Jun 1 at 23:43









Stephen Rauch

3,75061630




3,75061630










asked Jun 1 at 4:28









ChrisChris

1311




1311











  • $begingroup$
    What does your program actually do?
    $endgroup$
    – Justin
    Jun 1 at 4:35










  • $begingroup$
    It's a Function to extract float values from price strings. Those strings can have different formats.
    $endgroup$
    – Chris
    Jun 1 at 4:40










  • $begingroup$
    So for 1.298,90 € should the output be 1298.9?
    $endgroup$
    – Justin
    Jun 1 at 4:47










  • $begingroup$
    Exactly. And then there are different currencies, different ways of displaying prices etc.
    $endgroup$
    – Chris
    Jun 1 at 5:22










  • $begingroup$
    I don't have experience with Python, but if I understood your problem correctly, isn't it the easiest way to just extract those characters that are a number and parse them as a float? Either with a simple regex or string functions.
    $endgroup$
    – Charmander
    Jun 2 at 11:52
















  • $begingroup$
    What does your program actually do?
    $endgroup$
    – Justin
    Jun 1 at 4:35










  • $begingroup$
    It's a Function to extract float values from price strings. Those strings can have different formats.
    $endgroup$
    – Chris
    Jun 1 at 4:40










  • $begingroup$
    So for 1.298,90 € should the output be 1298.9?
    $endgroup$
    – Justin
    Jun 1 at 4:47










  • $begingroup$
    Exactly. And then there are different currencies, different ways of displaying prices etc.
    $endgroup$
    – Chris
    Jun 1 at 5:22










  • $begingroup$
    I don't have experience with Python, but if I understood your problem correctly, isn't it the easiest way to just extract those characters that are a number and parse them as a float? Either with a simple regex or string functions.
    $endgroup$
    – Charmander
    Jun 2 at 11:52















$begingroup$
What does your program actually do?
$endgroup$
– Justin
Jun 1 at 4:35




$begingroup$
What does your program actually do?
$endgroup$
– Justin
Jun 1 at 4:35












$begingroup$
It's a Function to extract float values from price strings. Those strings can have different formats.
$endgroup$
– Chris
Jun 1 at 4:40




$begingroup$
It's a Function to extract float values from price strings. Those strings can have different formats.
$endgroup$
– Chris
Jun 1 at 4:40












$begingroup$
So for 1.298,90 € should the output be 1298.9?
$endgroup$
– Justin
Jun 1 at 4:47




$begingroup$
So for 1.298,90 € should the output be 1298.9?
$endgroup$
– Justin
Jun 1 at 4:47












$begingroup$
Exactly. And then there are different currencies, different ways of displaying prices etc.
$endgroup$
– Chris
Jun 1 at 5:22




$begingroup$
Exactly. And then there are different currencies, different ways of displaying prices etc.
$endgroup$
– Chris
Jun 1 at 5:22












$begingroup$
I don't have experience with Python, but if I understood your problem correctly, isn't it the easiest way to just extract those characters that are a number and parse them as a float? Either with a simple regex or string functions.
$endgroup$
– Charmander
Jun 2 at 11:52




$begingroup$
I don't have experience with Python, but if I understood your problem correctly, isn't it the easiest way to just extract those characters that are a number and parse them as a float? Either with a simple regex or string functions.
$endgroup$
– Charmander
Jun 2 at 11:52










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$




















    3












    $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
      Jun 1 at 16:53










    • $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
      Jun 1 at 17:42


















    3












    $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 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
      );



      );













      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 Jun 1 at 5:04









          ndpndp

          1,45299




          1,45299























              3












              $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
                Jun 1 at 16:53










              • $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
                Jun 1 at 17:42















              3












              $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
                Jun 1 at 16:53










              • $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
                Jun 1 at 17:42













              3












              3








              3





              $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 Jun 1 at 16:29









              PeilonrayzPeilonrayz

              29.2k445119




              29.2k445119











              • $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
                Jun 1 at 16:53










              • $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
                Jun 1 at 17:42
















              • $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
                Jun 1 at 16:53










              • $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
                Jun 1 at 17:42















              $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
              Jun 1 at 16:53




              $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
              Jun 1 at 16:53












              $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
              Jun 1 at 17:42




              $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
              Jun 1 at 17:42











              3












              $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 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$

















                3












                $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 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$















                  3












                  3








                  3





                  $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 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 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 Jun 2 at 11:41









                  Community

                  1




                  1










                  answered Jun 1 at 6:48









                  JustinJustin

                  1,705529




                  1,705529



























                      draft saved

                      draft discarded
















































                      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

                      How to write a 12-bar blues melodyI-IV-V blues progressionHow to play the bridges in a standard blues progressionHow does Gdim7 fit in C# minor?question on a certain chord progressionMusicology of Melody12 bar blues, spread rhythm: alternative to 6th chord to avoid finger stretchChord progressions/ Root key/ MelodiesHow to put chords (POP-EDM) under a given lead vocal melody (starting from a good knowledge in music theory)Are there “rules” for improvising with the minor pentatonic scale over 12-bar shuffle?Confusion about blues scale and chords

                      What if the end-user didn't have the required library?What is setup.py?What is a clean, pythonic way to have multiple constructors in Python?What does Ruby have that Python doesn't, and vice versa?What is the reason for having '//' in Python?How do I create a namespace package in Python?How to package shared objects that python modules depend on?setuptools vs. distutils: why is distutils still a thing?Navigation in Windows 10 vs code not going to virtualenv library when the same library is installed at user levelPython create package for local usePackaging a project that uses multiple python versionsWhy is permission denied on pip install except for when “--user” is included at end of command?

                      Esgonzo ibérico Índice Descrición Distribución Hábitat Ameazas Notas Véxase tamén "Acerca dos nomes dos anfibios e réptiles galegos""Chalcides bedriagai"Chalcides bedriagai en Carrascal, L. M. Salvador, A. (Eds). Enciclopedia virtual de los vertebrados españoles. Museo Nacional de Ciencias Naturales, Madrid. España.Fotos