1

The following python returns the CPU temperature of my Raspberry Pi running Raspian 8.0 jessie:

import commands
err, msg = commands.getstatusoutput('vcgencmd measure_temp')
if not err:
   print msg

returns "temp=47.2'C" with a standard single quote instead of the degrees symbol.

This gives me the temperature as a float and so I have something that works.

print float(msg.split('=')[1].split("'")[0])

returns the float 47.2

Question: But I wonder if there is a better, or more pythonic, or at least less hackish-looking way to do this?

note: I don't know how the OS builds the string, but the number could be variable in length, be > 100C or even have a minus sign, or possibly even have the trailing zero suppressed. See https://raspberrypi.stackexchange.com/search?q=+liquid+nitrogen

I have heard of regex but I don't normally do much with strings and have never tried to use it.

uhoh
  • 3,713
  • 6
  • 42
  • 95
  • 2
    Can't you just use `float(msg[5:-2])`? The text always starts with `temp=` and ends with `'C`, right? – Aran-Fey Mar 29 '18 at 18:40
  • "[I] have never tried to use it." Well now is a good moment to try! there are some simple tutorials online. Regex is one of the best ways to find something in a string – Liora Haydont Mar 29 '18 at 18:40
  • @Aran-Fey I don't know the rules that the OS uses to build the string. It might zero-supress, and the temperature can have more or less digits, and be negative, so, no that won't work. – uhoh Mar 29 '18 at 18:41
  • Regex could work with something like `re.sub(r".*=([0-9.]+)'.*", r'\1', "temp=47.3'C")` but I don't know if it's really necessary. It looks less hackish though. – Benoît Latinier Mar 29 '18 at 18:42
  • @LioraHaydont considering my previous comment (which I'll put into the question in a second) do you know that regex will indeed do this? – uhoh Mar 29 '18 at 18:42
  • 3
    The only assumptions I made are that the text starts with 5 useless characters (like `temp=`) and ends with 2 useless characters (like `'C`). Zero-padded or negative numbers are supported as long as those two assumptions are correct. – Aran-Fey Mar 29 '18 at 18:43
  • 3
    Possible duplicate of [How to extract a floating number from a string](https://stackoverflow.com/questions/4703390/how-to-extract-a-floating-number-from-a-string) – Liora Haydont Mar 29 '18 at 18:47
  • @Aran-Fey okay, (it's 3AM here I missed that) but I don't yet know if there could be other warnings or characters appended, so I can't guarantee there will always be exactly two characters after the float. – uhoh Mar 29 '18 at 18:48
  • I don't see how the [link provided](https://raspberrypi.stackexchange.com/search?q=+liquid+nitrogen) is relevant to the note which precedes it. – John Y Mar 29 '18 at 20:28
  • 1
    hey it's @uhoh from space! anyhow, heads-up that `commands` has been deprecated since 2.6, replaced by `subprocess`. – tedder42 Sep 07 '18 at 21:27
  • @tedder42 indeed! You are welcome to add an updated answer here if you are so inclined. I will try to do it at some point, but haven't been 2to3 qualified yet so it's better if someone who knows what they're doing does it. – uhoh Sep 08 '18 at 00:04

1 Answers1

1

Here is solution with regex, if you're interested:

import re

msg = "temp=47.2'C"
m = re.search(r'-?\d+\.?\d*', msg)
print(float(m.group()))  # 47.2
Grigoriy Mikhalkin
  • 5,035
  • 1
  • 18
  • 36
  • 1
    This does not look as scary as the other answers in the possible-dup link! Let me give it a quick try with some other possible messages. – uhoh Mar 29 '18 at 18:50
  • 1
    Yes this is better as it reliably returns a single float even if I add other floats to `msg`. I will read more about `.search()` rather than `.findall()` and also `.group()`. Not only does it solve my problem, but it is clean, concise, and educational! I am no longer afraid of regex! woohoo! Normally I would not accept so quickly, I'll check back again later for more answers, but this seems concise and yet robust for my purposes. – uhoh Mar 29 '18 at 18:55