Variable instead of file name used in spreadsheet for finding an audio file

OS : Win10
PsychoPy version (e.g. 1.84.x): 3.2.3
Standard Standalone? (y/n) If not then what?: y
What are you trying to achieve?: Trying to get a variable to work inside a spreadsheet

What did you try to make it work?:
From previous steps, the experiment associates a variable with a file name as a string e.g. Ast_Train1_Lau is associated with ‘tokens/Laugh/S01_NS_01.wav’. This is done for randomisation purposes. I’ve made them print so I know the association works.
The problem I’m having is that when I put the variable into the spreadsheet it doesn’t see the file name (string), it tries to find a file name with the variable e.g. Ast_Train1_Lau instead of tokens/Laugh/S01_NS_01.wav. I’ve tried this both with and without a $ in front of the spreadsheet.

What specifically went wrong when you tried that?:

Tries to find the variable as a file rather than the file name
Thanks for helping :slight_smile:
P.S.
Here is the association code and a couple previous steps in case this helps.



I have replicated this error here:




image

hi @Lloyd_Morgan, what is happening is that the variable you are trying to use is imported from your conditions file as a string. So, when you use the variable Fam_laugh, you will get the string version of the variable name, rather than the actual variable which points to the filename. To fix, you can use eval function, or more safely the literal_eval method from the ast library. E.g., in a code component

# Begin Experiment
from ast import literal_eval

Then, whereever you want to use your variable:

literal_eval(YOUR_VARIABLE)
1 Like

Thanks for the help. Not sure where to put the literal_eval(Your_Variable) bit. Using my Sample123 in the reply example, where should I add the command (aka in the code, spreadsheet or import audio file section)?

You would use literal_eval whereever you are using the Fam_laugh variable. The import part should be in the “Begin Experiment” tab of your code component.

Thanks, this is the change I made in the end to get it working. I couldn’t use literal_eval as it was bringing up error messages but eval() did the trick

Error messages for using literal_eval

Ok, that’s fine. eval is not as safe to use, should any malicious code get eval’d. Just something to be aware of, but if you know this cannot happen then you do not have to worry about it.