Writing data from Arduino, through VS Code, to a CSV file

About a month ago I was attempting to connect the Arduino UNO WIFI Rev2 to a temperature sensor and display that data on Visual Studio Code.

I finally managed to get the data to output perfectly, but now as I'm attempting to upload that data onto a CSV file the program keeps giving me this error code:

writer.writerow([myData])

ValueError: I/O operation on closed file.

Here is the code I'm attempting to execute:

import serial
import csv
import time

# Open the serial port
ser = serial.Serial('COM3', 9600)  # Replace 'COM3' with the name of your serial port

# Open the CSV file for writing
with open('Temp_data.csv', 'w', newline='') as csvfile:
    writer = csv.writer(csvfile)

# Read data from the serial port and write it to the CSV file
while True:
    if (ser.inWaiting()>0):
        myData = ser.readline()
        writer.writerow([myData])
        print(myData)

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.