Changing strings into integers
<p>I'm going crazy and I can not figure the right solution :(</p>

<p>How can I solve that problems. I have a loop and I can get diffrent types like:</p>

<pre><code>empty string
10
10K
2.3K
2.34K
2M
2.2M
2.23M
</code></pre>

<p>I need to change them into numbers:</p>

<pre><code>0
10
10000
2300
2340
2000000
2200000
2230000
</code></pre>

<p>A quick hack to get the floats:</p>

<pre><code>In [15]: powers = {'K': 10 ** 3, 'M': 10 ** 6}

In [16]: def f(s):
    ...:     try:
    ...:         if s[-1] in powers.keys():
    ...:            return float(s[:-1]) * powers[s[-1]]
    ...:         else:
    ...:            return float(s)
    ...:     except:
    ...:         return s
    ...:         

In [17]: map(f, ['', '10', '10K', '2.3K', '2.34K', '2M', '2.2M', '2.23M'])
Out[17]: ['', 10.0, 10000.0, 2300.0, 2340.0, 2000000.0, 2200000.0, 2230000.0]
</code></pre>

python regex python-2.7