Saving Data to a .CSV

A text file is useful for basic data storage and formatting but unfortunately cannot organise the data very well. An alternative to a .txt is CSV file (Comma Separated Values), this allows for the ease of entry of a text file but with the ability to have the data in rows and columns rather than in disarray in normal text.

CSV is similar to a Microsoft Excel file but it can be used in more than just MS Office products making it useful for Linux. A CSV file can be viewed in LibreOffice Calc, a spreadsheet-based program installed by default with Raspbian (Raspberry Pi OS). When data is sent from the Python script it’s commas determine the row and column of where the data is stored.

To store the data with a timestamp the Python datetime module must be used. This allows the Pi to access the current date when storing the data from the serial port. In the code below, a column was made called date and one row was added which contained the current time and date.

import datetime
import csv

time1 = datetime.datetime.now()
time1.strftime("%A %d %b %y %H") # strftime: date formatting

with open('MyCSV.csv', 'w', newline='') as file: # with is a better way to access a file
 write = csv.writer(file)
 write.writerow(['Date'])
 write.writerow([time1])

The screenshot below from LibreOffice shows the result.

CSV Screenshot
Date CSV

The Python script was updated to allow for serial input as well as adding additional columns for future data input such as wind speed and voltage generated by the turbine.

import datetime
import csv
import serial
import string

print ("Starting... ")
serialData = serial.Serial(port = '/dev/ttyS0', # ttyS0 is for the RPi 3
                           baudrate = 19200,
                           parity = serial.PARITY_NONE,
                           stopbits = serial.STOPBITS_ONE,
                           bytesize = serial.EIGHTBITS,
                           timeout = 100)
with open('MyCSV.csv', 'w') as file: # with is a better way to access a file
  write = csv.writer(file)
  write.writerow([' ', 'UPV', 'A. Bailey', 'J. Harding', 'P. Malone'])
  write.writerow([' ']) # adds a blank row
  write.writerow([' ', 'Date', 'Wind Direction', 'Wind Speed', 'Voltage', 'Current', 'Power'])
  write.writerow([' ']) # adds a blank row

while True:
  data = serialData.readline().strip() # Storing incoming data from serial channel to variable "data" and stripping string
  print(data) # displays in the terminal of Pi
  time1 = datetime.datetime.now()
  time1.strftime("%A %d %b %y %H") # strftime: date formatting
  write.writerow([' ', time1, data]) # leaves blank a column

The screen capture below shows all data required to be stored by the turbine in its appropriate columns.

CSV Full
Full Data CSV

Leave a comment