Python Forum

Full Version: Overwrite previous live data.
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,

I am trying to output the current code, which is working ok.
But i want the output to overwrite previous data instead of append.


import requests
import csv
import time
import schedule
from time import sleep
import sys
import csv
import io
starttime=time.time()

def task():
    with io.open('C:\\Campbellsci\\LoggerNet\\CR1000_Public.dat') as f:
        reader = csv.reader(f, delimiter=',')
        
        for field in reader:
            a = (field[6])
            print(a)
            
while True:
 try:
     task()
     time.sleep(0.25 - time.time() % 0.25)
     refresh()
 except:
    pass
    time.sleep(0)
else:
 time.sleep(0)
 refresh()
Here is the output:

Python 3.8.3 (tags/v3.8.3:6f8c832, May 13 2020, 22:37:02) [MSC v.1924 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license()" for more information.
>>> 
================== RESTART: C:\Users\Makada\Desktop\cr1000.py ==================
11.38377
11.38377
11.07566
11.07566
10.79836
10.49026
10.42863
10.42863
10.15134
(Oct-30-2020, 10:38 PM)Makada Wrote: [ -> ]But i want the output to overwrite previous data instead of append
Overwrite and append may mot be the right wording here,as you only print the output.
So can clean the screen cls(Window),then only show new data.
Also nice to have a way to exit out of loop eg KeyboardInterrupt.
So can write a refresh function.
import requests
import time
import schedule
from time import sleep
import sys
import csv
import io
from os import system, name

def refresh():
    # Windows
    if name == 'nt':
        _ = system('cls')
    # Mac and linux(here, os.name is 'posix')
    else:
        _ = system('clear')

starttime=time.time()
def task():
    with io.open('Mynew.csv') as f:
        reader = csv.reader(f, delimiter=',')
        for field in reader:
            a = (field[1])
            print(a)
while True:
    try:
        task()
        time.sleep(7 - time.time() % 3)
        refresh()
    except KeyboardInterrupt:
        sys.exit()
    else:
        refresh()
As you also have import schedule schedule installed,
then can it better to use it to do schedule than calculate with time.time and time.sleep.
Hi, thanks for the input.
I get an new window every update...