Reputation: 23
I'd like to write a python program that allows the user to key an it will write the time of the keystroke to a CSV file. E.g., a "data logger" program. The context is that we need to know the precise time that a whale dives below, so I'd like a function that when the User hits "D" it logs writes the time.
I was considering writing a function such as:
from from datetime import datetime
def D(): print datetime.now
... and then have the User hit "D()". But my problems are that such a program closes immediately upon running the script, rather than staying open to log further "D()" instructions from the User. Plus, the User has to hit D() instead of a more ergonomic "D"
Thanks
Upvotes: 2
Views: 256
Reputation: 363203
You can use time
and csv
modules.
import time
import csv
with open('your_file.csv', 'w') as f:
csv_writer = csv.writer(f)
s = ''
while True:
s = raw_input("Enter 'D' to write a log event ('q' to quit): ")
if s == 'D':
t = time.ctime()
csv_writer.writerow(['D press event', t])
print 'logged a D press event at {}'.format(t)
elif s == 'q':
break
elif s:
print "You entered {}, that wasn't a D.. ;)".format(s)
Upvotes: 2