Skip to content

Instantly share code, notes, and snippets.

@toymakerlabs
Last active March 16, 2017 18:23
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/c1140dad4343168500c292c17e162ad8 to your computer and use it in GitHub Desktop.
Save toymakerlabs/c1140dad4343168500c292c17e162ad8 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
csv_file = open(path,'a')
writer = csv.writer(csv_file)
writer.writerow([st,data])
## Python will convert \n to os.linesep
csv_file.close()
#loop through the event list and append the event
for event in events:
time.sleep(1)
log_to_file(event)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment