Python Forum
write data each second into file with timestamp
Thread Rating:
  • 1 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
write data each second into file with timestamp
#1


I want to create a file with each second a logging of the data with a timestamp. instead of always overwriting it like the script underneath does. Perhaps in csv ?

But how to import the data that this script is reading into such a file ? I can not find exactly what I'm looking for Blush (wrong terms for searching perhaps)

Thanks for any kind of help

#!/usr/bin/env python3
# coding=utf-8
"""
 ...
"""
import sys, time
from daemon3x import daemon3x
from configparser import SafeConfigParser
import smaem

#read configuration
parser = SafeConfigParser()
parser.read('/etc/smaemd/config')

smaemserials=parser.get('SMA-EM', 'serials')
serials=smaemserials.split(' ')
smavalues=parser.get('SMA-EM', 'values')
values=smavalues.split(' ')
pidfile=parser.get('DAEMON', 'pidfile')

class MyDaemon(daemon3x):
	def run(self):
		emparts = {}
		while True:
			emparts=smaem.readem()
			for serial in serials:
				#print(serial)
				#print(emparts['serial'])
				if serial==format(emparts['serial']):
					#print("match")
					for value in values:
						file = open("/run/shm/em-"+format(serial)+"-"+format(value), "w")
						file.write('%.4f' % emparts[value])
						file.close()

if __name__ == "__main__":
	daemon = MyDaemon(pidfile)
	if len(sys.argv) == 2:
		if 'start' == sys.argv[1]:
			daemon.start()
		elif 'stop' == sys.argv[1]:
			daemon.stop()
		elif 'restart' == sys.argv[1]:
			daemon.restart()
		else:
			print ("Unknown command")
			sys.exit(2)
		sys.exit(0)
	else:
		print ("usage: %s start|stop|restart" % sys.argv[0])
		print (pidfile)
		sys.exit(2)
Reply
#2
Once you initially create the file with the 'w' option, you would then access it for subsequent writing with the 'a' (append) option. See Input and Output (scroll down to section 7.2)
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply
#3
FYI - 'a' will create the file if it doesn't already exist.
so you don't need 'w' at all.
Reply
#4
Thanks guys.

Now for the timestamp
First I do the following :
from datetime import datetime

Now how do I merge datetime.now() into the output file? I tried

file.write('%.4f'+datetime.now() % emparts[value])
But I'm not seeing any timestamp anywhere? Tried multiple formats but I never found any timestamp. I can not use it in the write command?
Reply
#5
Here's a method that I use for saving with time stamp:
    def save_session(self):
        current_time = f'{time.ctime()}'
        current_time = current_time.replace(' ', '-')
        current_time = current_time.replace(':', '_')
        fname = f'debug-info-{current_time}.json'
        sessionfile = self.paths.datapath / fname
        with sessionfile.open('w') as f:
            json.dump(self.summary_info, f)
This creates file names like: debug-info-Fri-Dec--1-05_44_36-2017.json
Reply
#6
Hmm thanks but I was speaking of the content of the file

Si it would hold the data with each record having its timestamp. Sorry if I didn't explain it all to well
Reply
#7
You explained it well enough.
I didn't read it well enough.
Reply
#8
I've got an idea.

Looking at the openingspost script, you see it reads data from smaem(.py) if I'm not mistaking.

What if i define a character X as in
character X=time.strftime("%Y-%m-%d %H:%M:%S")
so that when i put some reference in the smaem file to that command, I can use it in the output to have it show the output i want ? like in date + data it reads out ?

as in:
file.write('%characterX' '%.4f' "\n" % emparts[value] % emparts[value])


I hope you understand what I mean, I looked into defining into python (documentation), but I think i got the terminology wrong ?
Reply
#9
I have tried the following without success:

file.write('{%Y-%m-%d %H:%M:%S}'.format(datetime.datetime.now())+'%.4f' "\n" % emparts[value])
I'm not getting any error though, it's just writing 1 value away in a file, but the file itsself is blank
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Last record in file doesn't write to newline gonksoup 3 366 Jan-22-2024, 12:56 PM
Last Post: deanhystad
  write to csv file problem jacksfrustration 11 1,375 Nov-09-2023, 01:56 PM
Last Post: deanhystad
  python Read each xlsx file and write it into csv with pipe delimiter mg24 4 1,316 Nov-09-2023, 10:56 AM
Last Post: mg24
  Timestamp of file changes if a share mapped to a server…. tester_V 34 3,714 Jul-04-2023, 05:19 AM
Last Post: tester_V
  How do I read and write a binary file in Python? blackears 6 6,021 Jun-06-2023, 06:37 PM
Last Post: rajeshgk
  Reading data from excel file –> process it >>then write to another excel output file Jennifer_Jone 0 1,048 Mar-14-2023, 07:59 PM
Last Post: Jennifer_Jone
  Read text file, modify it then write back Pavel_47 5 1,503 Feb-18-2023, 02:49 PM
Last Post: deanhystad
  how to read txt file, and write into excel with multiply sheet jacklee26 14 9,525 Jan-21-2023, 06:57 AM
Last Post: jacklee26
  Write sql data or CSV Data into parquet file mg24 2 2,356 Sep-26-2022, 08:21 AM
Last Post: ibreeden
  How to write in text file - indented block Joni_Engr 4 6,363 Jul-18-2022, 09:09 AM
Last Post: Hathemand

Forum Jump:

User Panel Messages

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