Python Forum
Value Error and Saving Data to .csv
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Value Error and Saving Data to .csv
#1
Hello,

I'm very new to Python, and I've been given some code for a study I'm working on, but I've been having some trouble getting it to run through. Right now things are set so that a .csv file is created with the headers for all the data I'll be collecting. There seems to be a problem with one of the last lines where Python is told to record the data. Included below is the code I've got for setting up the data file (thanks to those who helped me fix some issues with this first bit):

#initialise data file - refer to 'startInfo' above for participant number
dataHeader=["Participant","Age","Gender","Handedness","Trial","Cue Side", "SOA","Target Type","RT"]
with open("mydata.csv", "w") as dataFile:
    dataFile.write(",".join(dataHeader) + "\n")

#Create function to write data 
def recordData(pt,age,gender,hand,trialnum,cue,soa,val,rt):
    dataFile.write(",".join([str(pt),str(age),str(gender),str(hand),str(trialnum),str(cue),str(soa),str(val),str(rt)])+"\n")
    dataFile.flush()
And here is the latter part of the code that seems to be giving me trouble:

#loop for trial sequence
for i in range (numtrials):
    fixscreen()
    if cueside[i]==0:
        lcue()
    else:
        rcue()
    if soa[i]==0:
        short_soa()
    else:
        long_soa()
    if validity[i]==0:
        ltarg()
    else:
        rtarg()
    RTClock=core.Clock()
    keypress=event.waitKeys(keyList=["space","q"])
    RT=RTClock.getTime()
    if keypress[0][0]=="q":
        core.quit()
    else:
        recordData(str(startInfo[0]),str(startInfo[1]),str(startInfo[2]),str(startInfo[3]),i,cueside[i],soa[i],validity[i],RT)
Here is the error message I get when trying to run the study:
Error:
Traceback (most recent call last): File "C:\Users\Carol\Documents\COVAT3.py", line 190, in <module> recordData(str(startInfo[0]),str(startInfo[1]),str(startInfo[2]),str(startInfo[3]),i,cueside[i],soa[i],validity[i],RT) File "C:\Users\Carol\Documents\COVAT3.py", line 35, in recordData dataFile.write(",".join([str(pt),str(age),str(gender),str(hand),str(trialnum),str(cue),str(soa),str(val),str(rt)])+"\n") ValueError: I/O operation on closed file.
If anybody could advise me on what's wrong here I'd really appreciate it.

Thanks in advance.
Reply


Messages In This Thread
Value Error and Saving Data to .csv - by caroline_d_124 - Jan-06-2019, 10:03 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Need help formatting dataframe data before saving to CSV cubangt 16 6,053 Jul-01-2022, 12:54 PM
Last Post: cubangt
  pandas.errors.ParserError: Error tokenizing data. C error: Expected 9 fields in line Anldra12 9 15,463 Jun-15-2021, 08:16 AM
Last Post: Anldra12
  Error when Excelwriter saving a dataframe with datetime datatype with timezone klllmmm 3 13,555 Dec-08-2020, 11:37 AM
Last Post: Larz60+
  saving data from text file to CSV file in python having delimiter as space K11 1 2,430 Sep-11-2020, 06:28 AM
Last Post: bowlofred
  Reading serial data and saving to a file Mohan 1 7,606 May-25-2020, 04:18 PM
Last Post: pyzyx3qwerty
  hi guys, I got data reader error while pulling the data from yahoo gokulrajkmv 1 2,033 May-15-2020, 11:08 AM
Last Post: snippsat
  Saving and accessing data from different clients SheeppOSU 1 1,988 Jul-28-2019, 02:15 AM
Last Post: metulburr
  Openpyxl - while saving excel file getting error shubhamjainj 1 4,623 Apr-09-2019, 12:05 PM
Last Post: Larz60+
  Need help saving data into MySQL database reezalaq 0 2,411 Jun-03-2018, 07:50 PM
Last Post: reezalaq
  Data saving help zedwardson 5 78,172 Oct-03-2017, 07:11 PM
Last Post: zedwardson

Forum Jump:

User Panel Messages

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