Read excel dynamically based on participant

Hi Everyone,

I’m trying to have my experiment read the excel file of each participant at the end of the study, so I can get values that have been chosen during their trials. Based on what I’ve seen in other topics, using expInfo is the way?

The filename is in this format: ‘#_xyz.xlsx’, where # is entered manually for each participant through the dialogue window. The file path would be ‘data#_xyz.xlsx’.

I get invalid argument (see below) every time I try to run it, which I assume is xlrd struggling to read the expInfo part. Is there a way to include it properly? Possibly by using pandas instead of xlrd?

import xlrd
workbook = xlrd.open_workbook(r'$("data\"+$expInfo[‘participant’]+"_xyz.xlsx")')
OSError: [Errno 22] Invalid argument: '$("data\\"+$expInfo[‘participant’]+"_xyz.xlsx")'

Any pointers would be appreciated!


Hello Andi,

looks like you are using “intelligent” quotation marks around participant. Replace them with dumb ones ’

Best wishes Jens

Hi Jens,

thanks for you help, unfortunately that didn’t fix it. By using dumb ones I get syntax error and can’t start the experiment at all. I’ve tried playing around with different placements of both quotation marks, but to no avail.

Using $expInfo[‘participant’] in itself shouldn’t create any issues, right?


Hello Andi

Well, you might have more than one error :wink:

I am using Builder instead of Coder to program my PsychoPy-experiments. The following PsychoPy-experiment reads one of three files depending on the participant number from the folder data.

filename.psyexp (8.8 KB)
1_xyz.xlsx (8.3 KB)
2_xyz.xlsx (8.3 KB)
3_xyz.xlsx (8.4 KB)

Well, it might be worth to print out some values (participant, the combined filename aso.) to find the error.

BTW, don’t you need \\ instead of just \?

Do you need


or isn’t


the proper syntax? At least that works for me.

What does the

r do here?

I would create the filename including path-information and then read it.

fileName = "data\\" + expInfo['participant'] + "_xyz.xlsx"
workBook = xlrd.open_workbook(fileName)

Best wishes Jens

Hi Jens,

fileName = "data\\" + expInfo['participant'] + "_xyz.xlsx"
workBook = xlrd.open_workbook(fileName)

this worked for me, seems like my main issue was having the r in there, I printed the seperate values and they worked on their own. My inexperience with python was showing!

Thanks for all your help and the examples :slight_smile: