Search code examples
arduinoraspberry-pi4datastore

how to split date-time and data from same column in csv using python?


I am storing data from Arduino to RaspberryPi. My code is working well, but the date-time and data are logging in the first column together (picture 1), although those are shown separately in the RaspberryPi display (picture 2). How can I log those in a seperate column? I attached my code here.

import time,datetime
from datetime import datetime
import csv
import serial
arduino_port = "/dev/ttyACM0" 
baud = 9600 
fileName="Arduino1.csv" 
ser = serial.Serial(arduino_port, baud)
print("Connected to Arduino port:" + arduino_port)
file = open(fileName, "a")
print("Created file")

samples =float('inf') 
print_labels = False
line = 0 
print('Press Ctrl-C to quit...')
print('Time, CO2(ppm), Temp(C), Humi(%), Vis, IR, UV, pH')
print('-' *85)

while line <= samples:
    
    d=datetime.now().strftime('%Y-%m-%d %H:%M:%S')

    if print_labels:
        if line==0:
            print("Printing Column Headers")
        else:
            print("Line " + str(line) + ": writing...")
    getData=str(ser.readline())
    data=getData [1:] [1:-5]
    print(d, data)

    file = open(fileName, "a")
    file.write(d+ data+ "\n") 
    line = line+1

file.close()

enter image description here

enter image description here


Solution

  • try adding a comma after the Date-time string to separate the data to another column? The date-time and data appear to be separate in the RaspberryPi, but not comma separated, thus both are in the same column.

    Try this -

    file.write(d + "," + data + "\n")