Saving trial data using csv writer

MacOS High Sierra
PsychoPy version 1.90.3 (Builder)

I am trying to export data from one trial to a separate csv file with only two columns of selected data to be used later on in the experiment as stimuli. I first tried using TrialHandler but I wasn’t able to remove certain columns and rows from being saved (e.g. “extraInfo” components at the bottom of the csv).

Then I tried using the csv writer to select for just the two bits of information I wanted to save: “word” (a parameter from my conditions file) and “cue_input” (the key response to that word). I tried creating the file by using writer and appending it by adding rows at the end of each routine.

I currently have a code component with the following:

Begin Routine

import csv
import os.path

with open('~/Desktop/myfile.csv', 'w') as csvfile:
    writer = csv.writer(csvfile, delimiter = ',')

End Routine

with open('~/Desktop/myfile.csv', 'a') as csvfile:
    writer = csv.writer(csvfile, delimiter = ',')
    writer.writerows(zip(word, cue_input))

Currently it’s only creating the blank “myfile.csv” and not appending it at all or adding in any data. I did try putting the last chunk under “Each Frame” which succeeded in writing something into the file, however both the “word” data was only the same letter repeated in each row and the “cue_input” data was missing entries, so I put it back under “End Routine”.

Ideally, I would like myfile.csv to look something like this:
41%20PM

Any help would be greatly appreciated!

So this sounds like just a single value. What you need to do is maintain a list of values from each trial (and the corresponding responses), and zip those together.

i.e. in the “begin routine” tab, something like:

if your_loop_name.thisN == 0 # only on the first trial
    word_list = [] # initialise blank lists
    cue_input_list = []

Then in the “End routine” tab, something like:

# grow the lists on each trial:
word_list.append(word)
cue_input_list.append(cue_input)
1 Like

Worked beautifully. Thank you.