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
#2
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).
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
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.
Reply
#4
(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!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  writing and running code in vscode without saving it akbarza 1 346 Jan-11-2024, 02:59 PM
Last Post: deanhystad
  Issue in writing sql data into csv for decimal value to scientific notation mg24 8 2,879 Dec-06-2022, 11:09 AM
Last Post: mg24
  Create a function for writing to SQL data to csv mg24 4 1,111 Oct-01-2022, 04:30 AM
Last Post: mg24
  Writing string to file results in one character per line RB76SFJPsJJDu3bMnwYM 4 1,308 Sep-27-2022, 01:38 PM
Last Post: buran
  Writing to json file ebolisa 1 970 Jul-17-2022, 04:51 PM
Last Post: deanhystad
  Writing to External File DaveG 9 2,410 Mar-30-2022, 06:25 AM
Last Post: bowlofred
  Need Help writing data into Excel format ajitnayak87 8 2,438 Feb-04-2022, 03:00 AM
Last Post: Jeff_t
  Fastest Way of Writing/Reading Data JamesA 1 2,139 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,204 Mar-29-2021, 09:36 PM
Last Post: Larz60+
  Writing to file ends incorrectly project_science 4 2,641 Jan-06-2021, 06:39 PM
Last Post: bowlofred

Forum Jump:

User Panel Messages

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