Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save toymakerlabs/90dfdea0037fc07e2382d845b69444d7 to your computer and use it in GitHub Desktop.
Save toymakerlabs/90dfdea0037fc07e2382d845b69444d7 to your computer and use it in GitHub Desktop.
import time,datetime,csv
#http://stackoverflow.com/questions/15578331/save-list-of-ordered-tuples-as-csv
#fake set of events. This might come from a sensor later on
events = ["event1","event2","event3","event4","event5"]
def log_to_file(data):
#set the path to the file. Right now it needs to be in the same folder.
# CSV means comma separated value
path = "log.csv"
#get the current time
ts = time.time()
#this makes a timestamp that we can add as a string, so it looks like: 2012-12-15 01:21:05
st = datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S')
#opens the CSV file
with open(path,'a') as csv_file:
writer = csv.writer(csv_file)
#writes a new row with the timestamp, and the data supplied from the function paramters
writer.writerow([st,data])
#loop through the event list and append the event
for event in events:
log_to_file(event)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment