Python - Convert string-numeric to float
<p>I have the following string numeric values, and need to keep only the digit and decimals. I just can't find a right regular expression for this. </p>

<pre><code>s = [
      "12.45-280", # need to convert to 12.45280
      "A10.4B2", # need to convert to 10.42
]
</code></pre>

<p>You could go for a combination of <code>locale</code> and regular expressions:</p>

<pre><code>import re, locale
from locale import atof

# or whatever else
locale.setlocale(locale.LC_NUMERIC, 'en_GB.UTF-8')

s = [
      "12.45-280", # need to convert to 12.45280
      "A10.4B2", # need to convert to 10.42
]

rx = re.compile(r'[A-Z-]+')

def convert(item):
    """
    Try to convert the item to a float
    """
    try:
        return atof(rx.sub('', item))
    except:
        return None

converted = [match
            for item in s
            for match in [convert(item)]
            if match]

print(converted)
# [12.4528, 10.42]
</code></pre>

python regex typeconverter