Python Forum
Help with Code (Writing Data File)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help with Code (Writing Data File)
#1
Hello,

I'm extremely new to Python and I was recently given some code for a study I'm working on, however it seems to contain some errors. I was able to fix some of the minor ones, but I'm still unable to get it running all the way through.

I'm currently getting errors regarding how the data file is being written. Posted below is the code leading up to and including the section that's been giving me trouble. Hopefully this is enough context. If I could have any advice on what the issue might be here and on any other errors in this section of the script I'd greatly appreciate it.

Thanks in advance.

from psychopy import visual, gui, core, event
import random as random
import os

#set directory path
path=os.getcwd()

#set number of trials
numtrials=100

#make a GUI to collect participant number
COVATDlg=gui.Dlg(title="Covert Orienting Task")
COVATDlg.addText('Participant Number', color='black')
COVATDlg.addField('Participant ID:')
COVATDlg.addField('Age:')
COVATDlg.addField('Gender:',choices=['male','female'])
COVATDlg.addField('Handedness:',choices=['left','right','both'])
COVATDlg.show()
if gui.OK:
    startInfo=COVATDlg.data
    print('startInfo')
else: print('cancelled')

#set window

screen_size=(1366,768)
win=visual.Window(size=screen_size,color=(-1,-1,-1),units='pix',fullscr=True)

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

#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()
Reply


Messages In This Thread
Help with Code (Writing Data File) - by caroline_d_124 - Jan-06-2019, 01:12 AM
RE: Help with Code (Writing Data File) - by scidam - Jan-06-2019, 02:50 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Help with writing monitored data to mysql upon change of one particular variable donottrackmymetadata 3 381 Apr-18-2024, 09:55 PM
Last Post: deanhystad
  writing and running code in vscode without saving it akbarza 1 440 Jan-11-2024, 02:59 PM
Last Post: deanhystad
  Issue in writing sql data into csv for decimal value to scientific notation mg24 8 3,168 Dec-06-2022, 11:09 AM
Last Post: mg24
  Create a function for writing to SQL data to csv mg24 4 1,237 Oct-01-2022, 04:30 AM
Last Post: mg24
  Writing string to file results in one character per line RB76SFJPsJJDu3bMnwYM 4 1,441 Sep-27-2022, 01:38 PM
Last Post: buran
  Writing to json file ebolisa 1 1,053 Jul-17-2022, 04:51 PM
Last Post: deanhystad
  Writing to External File DaveG 9 2,596 Mar-30-2022, 06:25 AM
Last Post: bowlofred
  Need Help writing data into Excel format ajitnayak87 8 2,616 Feb-04-2022, 03:00 AM
Last Post: Jeff_t
  Fastest Way of Writing/Reading Data JamesA 1 2,241 Jul-27-2021, 03:52 PM
Last Post: Larz60+
Smile Set 'Time' format cell when writing data to excel and not 'custom' limors 3 6,425 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