Python Forum

Full Version: Help with Code (Writing Data File)
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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()
If you are getting errors, it would be really helpful to us if you could post the full text of the errors (in output tags, like the python tags you used for your code).
I just installed psychopy with lots of its dependencies...
Everything works fine until line 30.
If you looked at startInfo , you could find that it is just a list of strings.
So, the dataFile variable is a string too. Attempting to call nonexistent .write method (line #32) for the string
rises the error. I got the following:
Error:
Traceback (most recent call last): File "test.py", line 32, in <module> dataFile.write(",".join(dataHeader) + "\n") AttributeError: 'str' object has no attribute 'write'
If you want to write data into a csv file, I would suggest you to use the Pandas package,
data frames and magic method .to_csv. Using Pandas wouldn't be an overkill, if we take into
account tons of dependencies of the pychopy package:)

Alternative way is to create file and write to it directly, e.g. insert the
following piece of code instead of lines #30-33:

dataHeader=["Participant","Age","Gender","Handedness","Trial","Cue Side", "SOA","Target Type","RT"]
with open("mydata.csv", "w") as dataFile:
    dataFile.write(",".join(dataHeader) + "\n")
Note that I've never used psychopy before. So, it might also include
its own mechanism of storing data to files and we don't need to build such things from scratch.
(Jan-06-2019, 02:50 AM)scidam Wrote: [ -> ]I just installed psychopy with lots of its dependencies...
Everything works fine until line 30.
If you looked at startInfo , you could find that it is just a list of strings.
So, the dataFile variable is a string too. Attempting to call nonexistent .write method (line #32) for the string
rises the error. I got the following:
Error:
Traceback (most recent call last): File "test.py", line 32, in <module> dataFile.write(",".join(dataHeader) + "\n") AttributeError: 'str' object has no attribute 'write'
If you want to write data into a csv file, I would suggest you to use the Pandas package,
data frames and magic method .to_csv. Using Pandas wouldn't be an overkill, if we take into
account tons of dependencies of the pychopy package:)

Alternative way is to create file and write to it directly, e.g. insert the
following piece of code instead of lines #30-33:

dataHeader=["Participant","Age","Gender","Handedness","Trial","Cue Side", "SOA","Target Type","RT"]
with open("mydata.csv", "w") as dataFile:
    dataFile.write(",".join(dataHeader) + "\n")
Note that I've never used psychopy before. So, it might also include
its own mechanism of storing data to files and we don't need to build such things from scratch.

Thanks, this was very helpful!