| Reference | Downloads | Github

Read Sheet From .xlsx based on expInfo['group']

OS (e.g. Win10):
PsychoPy version (e.g. 1.84.3):
**Standard Standalone? (y)
What are you trying to achieve?:

Currently, I load a stimulus file in the “Begin Experiment” part of my code as follows:

infile = 'stimlist.xlsx’
inbook = xlrd.open_workbook(infile)
insheet = inbook.sheet_by_index(0)

…the first 2 lines give me a file called stimlist.xlsx, and the 3rd line gives me the first sheet. However, I’d like to do something a bit more flexible, and specify the sheet based on the value from expInfo[‘group’]. For example, if group =0 I’ll get sheet 1, if group=1, I’ll get sheet 2, if group=3 I’ll get sheet 3 and so on.
What did you try to make it work?:
I’ve tried various ways, including the following:
insheet = inbook.sheet_by_index(%) %$expInfo[‘group’]
insheet = inbook.sheet_by_index ($expInfo[‘group’])
insheet = inbook.sheet_by_index(data.importConditions(expInfo[‘group’]))

…the first two yield an “Error: Invalid syntax” message, and the final one yields an “ImportError”:Condition Files could not be found" message

Is there a way of doing this? I’ve searched the forum and can’t find a clear solution, and I think having one would be very valuable.

Thanks a lot - happy to elaborate further.

insheet = inbook.sheet_by_index(expInfo[‘group’] + 1)

or possibly this if the value needs to be converted from text to a number:

insheet = inbook.sheet_by_index(int(expInfo[‘group’]) + 1)

You don’t use $ symbols in Python code. That is just an indicator in the Builder interface that something should be regarded as code rather than literal text. It gets stripped out and isn’t a part of the Python language itself.

Thank you very much, Michael! The second one works - Brilliant, and very valuable!