Function to extract float from different price patterns

Which languages would be most useful in Europe at the end of the 19th century?

Bb13b9 confusion

Is it possible to fly backward if you have a 'really strong' headwind?

How to hide an urban landmark?

Are polynomials with the same roots identical?

Why am I getting a strange double quote (“) in Open Office instead of the ordinary one (")?

Russian word for a male zebra

Is it safe to change the harddrive power feature so that it never turns off?

Heap allocation on microcontroller

Is there a set of positive integers of density 1 which contains no infinite arithmetic progression?

How to ensure color fidelity of the same file on two computers?

Fermat's statement about the ancients: How serious was he?

Why didn't Voldemort recognize that Dumbledore was affected by his curse?

What is the maximum number of net attacks that one can make in a round?

sed + add word before string only if not exists

Who won a Game of Bar Dice?

Why does Sin[b-a] simplify to -Sin[a-b]?

Let M and N be single-digit integers. If the product 2M5 x 13N is divisible by 36, how many ordered pairs (M,N) are possible?

With Ubuntu 18.04, how can I have a hot corner that locks the computer?

HR woman suggesting me I should not hang out with the coworker

How to communicate to my GM that not being allowed to use stealth isn't fun for me?

Is it possible to have a wealthy country without a middle class?

Writing an augmented sixth chord on the flattened supertonic

Entire circuit dead after GFCI outlet



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









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









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









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('^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









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 Jun 1 at 23:43









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 Jun 1 at 4:28









ChrisChris

1311




1311




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



      );






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









          ndpndp

          1,44299




          1,44299























              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

              28.3k344118




              28.3k344118











              • $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,331427




                  1,331427




















                      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

                      Category:9 (number) SubcategoriesMedia in category "9 (number)"Navigation menuUpload mediaGND ID: 4485639-8Library of Congress authority ID: sh85091979ReasonatorScholiaStatistics

                      Circuit construction for execution of conditional statements using least significant bitHow are two different registers being used as “control”?How exactly is the stated composite state of the two registers being produced using the $R_zz$ controlled rotations?Efficiently performing controlled rotations in HHLWould this quantum algorithm implementation work?How to prepare a superposed states of odd integers from $1$ to $sqrtN$?Why is this implementation of the order finding algorithm not working?Circuit construction for Hamiltonian simulationHow can I invert the least significant bit of a certain term of a superposed state?Implementing an oracleImplementing a controlled sum operation

                      Magento 2 “No Payment Methods” in Admin New OrderHow to integrate Paypal Express Checkout with the Magento APIMagento 1.5 - Sales > Order > edit order and shipping methods disappearAuto Invoice Check/Money Order Payment methodAdd more simple payment methods?Shipping methods not showingWhat should I do to change payment methods if changing the configuration has no effects?1.9 - No Payment Methods showing upMy Payment Methods not Showing for downloadable/virtual product when checkout?Magento2 API to access internal payment methodHow to call an existing payment methods in the registration form?