Python Forum
Editing a txt document using python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Editing a txt document using python
#1
I have a program that monitors a DHT11 temperature sensor and logs the data to a txt document. The program loops every 3 minutes, so it gets a new line quite often. I'd like to have it purge out the old stuff, maybe delete the first line and add a new last line. Another option I would consider is to bulk delete several lines every hour, using a different program if necessary.

import Adafruit_DHT
import time
from gpiozero import LED
import requests
from datetime import datetime

DHT_SENSOR1 = Adafruit_DHT.DHT11
DHT_PIN1 = 14
LED1 = LED(21)  #Pulses as long as program is running
LED2 = LED(16)  #Amber on light

LED2.on()
with open("/home/pi/LittleFridgeLog.txt", "a") as file:
    results = "Monitor service restarted" + "\n"
    file.write(results)

#r = requests.post("https://bit.ly/EDITEDFORPRIVACY")  #Message that service has started
r = requests.post("https://joinjoaomgcd.appspot.com/_ah/api/messaging/v1/sendPush?deviceId=group.android&text=LFTempMonitorStart&apikey=EDITEDFORPRIVACY")
while True:    
    humidity1, temperature1 = Adafruit_DHT.read_retry(DHT_SENSOR1, DHT_PIN1)  #Little fridge
    LittleFridgeF = (temperature1 * 9/5) + 32
    LittleFridgeF = round(LittleFridgeF,2)
    now = datetime.now()
    current_time = now.strftime("%H:%M:%S")
    
    # Log time and temp to text file
    with open("/home/pi/LittleFridgeLog.txt", "a") as file:
        results = current_time + " Little Fridge:" + str(LittleFridgeF) + "\n"
        file.write(results)
    
    if LittleFridgeF < 40:  #Less than this temp is good.  Blue light on.
        print(current_time)
        print("Little Fridge:", LittleFridgeF)
        print("")
        LED1.blink(.05,5)

    else:
        LED1.blink(.3,.3)
        print(current_time)
        print("Little Fridge overtemp:", LittleFridgeF)
        print("")
        alert = "overtemplittlefridge " + str(LittleFridgeF)
        base = "autoremotejoaomgcd.appspot.com/sendmessage?key=EDITEDFORPRIVACY&message="
        alerturl = "https://"+base+alert
        r = requests.post(alerturl)
    
        
    time.sleep(180)
time.sleep(1)
Can this be done?
Reply
#2
Instead of appending, read the file in memory, skip/remove the lines at the top of the file and then add new data. Overwrite the existing file.
This said using sqlite3 database may be more convenient to work with than csv/txt file
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
Actually, I would really like a google sheet to be the log for this. From there, I can use Tasker to get data and/or edit the sheet. I've never used sqlite.

I have no idea HOW to do what you suggest!
Reply
#4
I figured it out. I will try to post the code tomorrow after I implement it in my projects.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to get first 5 images form the document using Python BeautifulSoup sarath_unrelax 0 1,615 Dec-19-2019, 07:13 AM
Last Post: sarath_unrelax
  How to extrac a data table from Pdf document using Python Amit0503 1 2,202 May-15-2019, 05:38 AM
Last Post: Larz60+
  [split] Python Guide / Document? kumatul 2 2,693 Jan-16-2019, 12:49 AM
Last Post: micseydel
  Python Guide / Document? Qui_Ten 1 2,781 Jan-13-2019, 07:50 AM
Last Post: Gribouillis
  Can python be used to search a word document for combinations of 6 digits? gkirt1053 2 2,753 Nov-15-2018, 06:22 PM
Last Post: gkirt1053
  Text editing with python UK_SPB 1 2,324 Nov-26-2017, 08:31 AM
Last Post: wavic

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020