I am using python to collect temperature data but only want to store the last 24 hours of data.
I am currently generating my .csv file with this
while True:
tempC = mcp.temperature
tempF = tempC * 9 / 5 + 32
timestamp = datetime.datetime.now().strftime("%y-%m-%d %H:%M ")
f = open("24hr.csv", "a")
f.write(timestamp)
f.write(',{}'.format(tempF))
f.write("\n")
f.close()
The .csv looks like this
The .csv this outputs looks like this
18-12-13 10:58 ,44.7125
18-12-13 11:03 ,44.6
18-12-13 11:08 ,44.6
18-12-13 11:13 ,44.4875
18-12-13 11:18 ,44.6
18-12-13 11:23 ,44.4875
18-12-13 11:28 ,44.7125
I don't want to roll over, just keep the last 24 hours of data. Since I am sampling data every 5 minutes I should end up with 144 lines in my CSV after 24 hours. so if I use readlines() I can tell how many lines I have but how do I get rid of any lines that are older than 24 hours? This is what I came up with which obviously doesn't work. Suggestions?
f = open("24hr.csv","r")
lines = f.readlines()
f.close()
if lines => 144:
f = open("24hr.csv","w")
for line in lines:
if line <= "timestamp"+","+"tempF"+\n":
f.write(line)
f.close()