Parse currency into numbers in Python
<p>I just learnt from <a href="https://stackoverflow.com/questions/37579712/format-numbers-as-currency-in-python">Format numbers as currency in Python</a> that the Python module <a href="http://babel.pocoo.org/en/latest/index.html" rel="noreferrer">babel</a> provides <a href="http://babel.pocoo.org/en/latest/numbers.html" rel="noreferrer"><code>babel.numbers.format_currency</code></a> to format numbers as currency. For instance,</p>

<pre><code>from babel.numbers import format_currency

s = format_currency(123456.789, 'USD', locale='en_US')  # u'$123,456.79'
s = format_currency(123456.789, 'EUR', locale='fr_FR')  # u'123\xa0456,79\xa0\u20ac'
</code></pre>

<p>How about the reverse, from currency to numbers, such as <code>$123,456,789.00</code> --> <code>123456789</code>? <code>babel</code> provides <a href="http://babel.pocoo.org/en/latest/api/numbers.html#number-parsing" rel="noreferrer"><code>babel.numbers.parse_number</code></a> to parse local numbers, but I didn't found something like <code>parse_currency</code>. So, what is the ideal way to parse local currency into numbers?</p>

<hr>

<p>I went through <a href="https://stackoverflow.com/questions/1450897/python-removing-characters-except-digits-from-string">Python: removing characters except digits from string</a>.</p>

<pre><code># Way 1
import string
all=string.maketrans('','')
nodigs=all.translate(all, string.digits)

s = '$123,456.79'
n = s.translate(all, nodigs)    # 12345679, lost `.`

# Way 2
import re
n = re.sub("\D", "", s)         # 12345679
</code></pre>

<p>It doesn't take care the decimal separator <code>.</code>.</p>

<hr>

<p>Remove all non-numeric characters, except for <code>.</code>, from a string (refer to <a href="https://stackoverflow.com/questions/947776/strip-all-non-numeric-characters-except-for-from-a-string-in-python">here</a>),</p>

<pre><code>import re

# Way 1:
s = '$123,456.79'
n = re.sub("[^0-9|.]", "", s)   # 123456.79

# Way 2:
non_decimal = re.compile(r'[^\d.]+')
s = '$123,456.79'
n = non_decimal.sub('', s)      # 123456.79
</code></pre>

<p>It does process the decimal separator <code>.</code>.</p>

<hr>

<p>But the above solutions don't work when coming to, for instance,</p>

<pre><code>from babel.numbers import format_currency
s = format_currency(123456.789, 'EUR', locale='fr_FR')  # u'123\xa0456,79\xa0\u20ac'
new_s = s.encode('utf-8') # 123 456,79 €
</code></pre>

<p>As you can see, the format of currency varies. <strong>What is the ideal way to parse currency into numbers in a general way?</strong></p>