Python Forum
Overwrite previous live data.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Overwrite previous live data.
#1
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
Reply
#2
(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.
Reply
#3
Hi, thanks for the input.
I get an new window every update...
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to plot data from live datasource? Makada 14 2,870 Sep-06-2020, 07:13 PM
Last Post: Makada
  Python animate live plotting fetching data from Mysql Table dhirajm 6 1,221 Apr-24-2020, 05:07 PM
Last Post: dhirajm
  How to overwrite file SriRajesh 3 2,149 Apr-14-2020, 12:57 PM
Last Post: steve_shambles
  Pulling & Reading Date from UDF that Compare it to Live Data firebird 4 1,181 Jul-20-2019, 09:30 AM
Last Post: snippsat
  Create simple live plot of stock data dram 1 1,350 May-28-2019, 06:24 AM
Last Post: heiner55
  Dict overwrite previous list vincentspm 1 1,133 Mar-20-2019, 12:31 PM
Last Post: scidam
  Run function in parallel but inherite dynamic data from the previous function atizva 4 1,794 Jul-11-2018, 06:39 AM
Last Post: volcano63
  overwrite data using openpyxl BNB 0 8,386 Jun-21-2017, 11:31 AM
Last Post: BNB

Forum Jump:

User Panel Messages

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