Skip to content

Datalogging with Python

Brendan Smith edited this page Aug 31, 2016 · 10 revisions

##Overview With a sensor and our time established, we have the groundwork necessary to start writing sensor log files. For this, we need to get the time inside a python script and then write our information to a .csv file.

##Getting the time with Python Our Raspberry Pi now knows the time without the internet, and we can use the python library datetime to get the current time of the day.

If you have not already acquainted yourself with the Python interpreter, see Getting acquainted with the Python interpreter.

>>> from datetime import datetime
>>> datetime.now()
~ ~ ~
>>> datetime.now().date()
~ ~ ~
>>> datetime.now().strftime("%Y_%m_%d")
~ ~ ~

Datetime will pull the local date from the computer, and store it as a "python object". From there, the various date fields (i.e. month, day of the week) can be accessed.

Strftime is short for "string format time" and is very commonly used amongst programming languages. It is not important to learn specific syntax, but Google, Stack Overflow, or the documentation will be very helpful to find a particular format.

##Python CSV writer

Clone this wiki locally