So I'm writing a little script in python to read the temperature of built-in core sensor(Raspberry Pi) + print it on the screen + write the data to the .txt
file. Everything is working pretty much, but I have two problems which I couldn't solve and I have spent like 3-4 hours on it. Fully output of that command is for example: temp=39.0'C and I need only the number from that string --> 39.0. So I'm trying to replace characters and part of the string that I don't need and make it number float.
return temp.replace("temp=", " ").replace("'C", "") My problem is when I'm running this code I still have 1 space before my number and that's because I'm replacing "temp=" with " ", but if I delete that space second replace becomes part of the string and it doesn't do its job anymore, in my opinion, there's too many --> """""'""" of those characters and program gets confused. But how can I solve it? Second problem how can I change it to float because, in my opinion, those numbers are still part of the string? Please help because I am so frustrated that such little script takes so much time to get it working.
import re
import os
import time
def measure_temp():
temp = os.popen("vcgencmd measure_temp").readline()
return temp.replace("temp=", " ").replace("'C", "")
while True:
temperature = measure_temp()
print(temperature)
f = open("pythonLog.txt", "a")
f.write (temperature)
f.close()
time.sleep(1)