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

**URL:** <https://discourse.psychopy.org/t/read-sheet-from-xlsx-based-on-expinfo-group/3771>\
**Category:** Builder\
**Created:** [January 18, 2018, 10:47pm UTC](https://discourse.psychopy.org/t/read-sheet-from-xlsx-based-on-expinfo-group/3771 "2018-01-18T22:47:15Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ryan\_Blything](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/ryan_blything/32/4204_2.png) [@Ryan\_Blything](https://discourse.psychopy.org/u/Ryan_Blything)\
**Post date:** [January 18, 2018, 10:47pm UTC](https://discourse.psychopy.org/t/read-sheet-from-xlsx-based-on-expinfo-group/3771/1 "2018-01-18T22:47:15Z")

</div>

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

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.  
Ryan

---

<div class="post-metadata">

**Author:** ![Michael](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/michael/32/16_2.png) [@Michael](https://discourse.psychopy.org/u/Michael)\
**Post date:** [January 18, 2018, 11:33pm UTC](https://discourse.psychopy.org/t/read-sheet-from-xlsx-based-on-expinfo-group/3771/2 "2018-01-18T23:33:32Z")

</div>

```
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.

---

<div class="post-metadata">

**Author:** ![Ryan\_Blything](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/ryan_blything/32/4204_2.png) [@Ryan\_Blything](https://discourse.psychopy.org/u/Ryan_Blything)\
**Post date:** [January 19, 2018, 12:36am UTC](https://discourse.psychopy.org/t/read-sheet-from-xlsx-based-on-expinfo-group/3771/3 "2018-01-19T00:36:43Z")

</div>

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