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

(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)
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)
FYI - 'a' will create the file if it doesn't already exist.
so you don't need 'w' at all.
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?
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
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
You explained it well enough.
I didn't read it well enough.
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 ?
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