Python Forum
Trying to Get Arduino sensor data over to excel using Python.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Trying to Get Arduino sensor data over to excel using Python.
#2
The reason this didn't work:
overall=list(zip(dates,instance,sensor_data))
Is that sensor_data is a list of lists so you get date, time, [sensor, data, as, a, list]. You need to flatten the sensor data. This is most easily done while the data is collected.
import csv
from datetime import datetime

class Serial:
    """My fake serial port listening to my imaginary Arduino"""
    def __init__(self, *args, **kwargs):
        print("opening", args, kwargs)

    def close(self):
        print("closing")

    def __enter__(self, *args, **kwargs):
        return self

    def __exit__(self, *args, **kwargs):
        self.close()

    def readline(self):
        return b"1,2,3,4,5\r\n"

labels = ["date", "time", "s1", "s2", "s3", "s4", "s5"]
samples = 20
rows = []

with Serial("COM3", 9600) as ser:
    for _ in range(samples):
        data = ser.readline()[:-2].decode('utf-8').split(",")
        now = datetime.now()
        rows.append([
            now.strftime('%m/%d/%Y'),
            now.strftime('%H:%M:%S'),
            *data
        ])

with open("test.csv", "w", encoding = 'UTF8', newline= '') as csv_file:
    writer = csv.writer(csv_file)
    writer.writerow(labels)
    writer.writerows(rows)
You don't need to use the csv library to write a csv file. Since the data is already comma delimited, you are just adding work.
labels = "date,time,s1,s2,s3,s4,s5\n"
samples = 20
rows = []

with Serial("COM3", 9600) as ser:
    for _ in range(samples):
        data = ser.readline()[:-2].decode('utf-8')
        now = datetime.now()
        rows.append("{},{},{}\n".format(
            now.strftime('%m/%d/%Y'),
            now.strftime('%H:%M:%S'),
            data))

with open("test.csv", "w", encoding = 'UTF8') as csv_file:
    csv_file.write(labels)
    csv_file.writelines(rows)
I don't think it makes sense to store all the data and then write it to the CSV file. Write the data as it is read. I would write your code like this:
labels = "date,time,s1,s2,s3,s4,s5\n"
samples = 20
with Serial("COM3", 9600) as ser:
    with open("test.csv", "w", encoding = 'UTF8', newline= '') as csv_file:
        csv_file.write(labels)
        for _ in range(samples):
            data = ser.readline()[:-2].decode('utf-8')
            now = datetime.now()
            row = "{},{},{}\n".format(
                now.strftime('%m/%d/%Y'),
                now.strftime('%H:%M:%S'),
                data)
            csv_file.write(row)
Why are you making separate columns for date and time. This makes date and time difficult to work with. Your date format is also a poor choice. I know month/day/year is a common convention, but it is worthless for sorting or comparison. I would have a single datetime column instead and use the default format.
labels = "time,s1,s2,s3,s4,s5\n"
samples = 20
with Serial("COM3", 9600) as ser:
    with open("test.csv", "w", encoding = 'UTF8', newline= '') as csv_file:
        csv_file.write(labels)
        for _ in range(samples):
            data = ser.readline()[:-2].decode('utf-8')
            csv_file.write(f"{datetime.now()},{data}\n")
j.crater likes this post
Reply


Messages In This Thread
RE: Trying to Get Arduino sensor data over to excel using Python. - by deanhystad - Dec-01-2022, 01:52 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Python error on mentioned Arduino port name dghosal 5 880 Aug-22-2023, 04:54 PM
Last Post: deanhystad
  Copy data from Excel and paste into Discord (Midjourney) Joe_Wright 4 2,104 Jun-06-2023, 05:49 PM
Last Post: rajeshgk
  Reading data from excel file –> process it >>then write to another excel output file Jennifer_Jone 0 1,132 Mar-14-2023, 07:59 PM
Last Post: Jennifer_Jone
  How to properly format rows and columns in excel data from parsed .txt blocks jh67 7 1,927 Dec-12-2022, 08:22 PM
Last Post: jh67
  Appending a row of data in an MS Excel file azizrasul 3 1,207 Nov-06-2022, 05:17 PM
Last Post: azizrasul
  Moving data from one Excel to another and finding maximum profit azizrasul 7 1,497 Oct-06-2022, 06:13 PM
Last Post: azizrasul
  get data from excel and find max/min Timmy94 1 1,128 Jul-27-2022, 08:23 AM
Last Post: Larz60+
  How to keep columns header on excel without change after export data to excel file? ahmedbarbary 0 1,181 May-03-2022, 05:46 PM
Last Post: ahmedbarbary
  Need Help writing data into Excel format ajitnayak87 8 2,566 Feb-04-2022, 03:00 AM
Last Post: Jeff_t
Smile Set 'Time' format cell when writing data to excel and not 'custom' limors 3 6,350 Mar-29-2021, 09:36 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020