How to extract number from text in python?
<p>I have the string listed below</p>

<pre><code>str = ['"Consumers_Of_Product": {"count": 13115}']
</code></pre>

<p>How can I extract the number 13115 as it will change, so that it will always equal var. In other words how do I extract this number from this string?</p>

<p>Most things I've done previously have not worked and I think that is due to the syntax. I'm running Python 2.7.</p>

<p>You have <a href="http://stackoverflow.com/questions/9949533/python-eval-vs-ast-literal-eval-vs-json-decode">3 options</a></p>

<p>But it's recommended to use <code>json</code> lib </p>

<pre><code>import json
s = ['"Consumers_Of_Product": {"count": 13115}']
s[0] = '{' + s[0] + '}'
my_var = json.loads(s[0]) # this is where you translate from string to dict
print my_var['Consumers_Of_Product']['count']
# 13115
</code></pre>

<blockquote>
  <p>remembering what <a href="http://stackoverflow.com/questions/39928255/how-to-extract-number-from-text-in-python/39928276#39928276">TigerhawkT3</a> said about why you shouldn't use <code>str</code></p>
</blockquote>

python string python-2.7 integer