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>Use <code>ast.literal_eval</code> on the single element in that list (which you shouldn't call <code>str</code> because it masks the built-in, and it isn't a string anyway), within curly braces (as it seems to be a dictionary element):</p>

<pre><code>&gt;&gt;&gt; import ast
&gt;&gt;&gt; s = ['"Consumers_Of_Product": {"count": 13115}']
&gt;&gt;&gt; ast.literal_eval('{{{}}}'.format(s[0]))
{'Consumers_Of_Product': {'count': 13115}}
</code></pre>

python string python-2.7 integer