Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You were on the right track using <code>re</code>; your best bet (assuming the decimal can be arbitrary, etc.) is something like this:</p> <pre><code>import re def temp(): command = "/opt/vc/bin/vcgencmd measure_temp" proc = subprocess.Popen(command, stdout=subprocess.PIPE, shell=True) output = proc.stdout.read() # Build the regex. Use () to capture the group; we want any number of # digits \d or decimal points \. that is preceded by temp= and # followed by 'C temp_regex = re.compile(r'temp=([\d\.]*)\'C') matches = re.findall(temp_regex, output) # now matches = ['54.1'] temp = float(matches[0]) return temp </code></pre> <p>The regex captures any combination of numbers and decimal places (e.g. <code>12.34.56</code> would get matched); you could restrict it if necessary to only allow a single decimal place, but that's more work than it appears to be worth, if you can trust that the data you're getting back is well-formed. If you do want the number to be more precise, you could compile the regex like this (for at least one numeral preceding the decimal place and exactly one following it):</p> <pre><code>temp_regex = re.compile(r'temp=(\d+.\d)\'C') </code></pre> <p>Again, we capture the expression using the parentheses (captured groups are <a href="http://docs.python.org/3/library/re.html#re.findall" rel="nofollow">returned</a> by findall), but this time, increase the specificity of what we're looking for. This will capture any number like <code>123.4</code> but not <code>.4</code> and not <code>123.</code> If you find that you need to broaden it out a bit but still want only one decimal place:</p> <pre><code>temp_regex = re.compile(r'temp=(\d+.\d+)\'C') </code></pre> <p>That will capture any number with at least one numeral proceeding and following the decimal, so <code>1234.5678</code> would match but <code>1234.</code> would not and <code>.1234</code> would not.</p> <p>As an alternative to using <code>re.findall()</code>, you might use <code>re.match()</code>, which returns <a href="http://docs.python.org/3/library/re.html#match-objects" rel="nofollow">match objects</a>. Then your usage would look something like this (using the direct method, rather than pre-compiling the string:</p> <pre><code>match = re.match(r'temp=(\d+.\d+)\'C', output) if match: temp = float(match.group(1)) # get the first matching group captured by () else: pass # You should add some error handling here </code></pre> <p>One of the things this makes clearer than the way I had <code>re.findall()</code> above is that if nothing is captured, you have an issue, and you need to figure out how to handle it.</p> <hr> <p>You can look at other ways to vary that up at <a href="http://www.regular-expressions.info/" rel="nofollow">Regular-Expressions.info</a>, easily the best site I've found on the web for a quick resource on the topic.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload