[open] Error in __init__ interrupts experiment
Hi,
I got a problem with OpenSesame Inlinescript. It reads a csv-file (showing no errors or bugs in any of the read lines) but it can't present them. At the first presentation of a part of the file-content, it interrupts the experiment. Only message is "'int' object has no attribute 'decode'". Within Debug-window, the content is presented as it should be (tested in Prepare-phase. But in Run-phase, as soon as the presentation should start, this error message shows up and interrupts the experiment.
The csv-files are coded in utf-8 without BOM (checked through Notepad++). The content of some fields got cyrillic characters, which don't cause any troubles in Prepare-phase. So I think it might be a canvas-problem, but how do I solve it, if it's been read in the right way?
Thank you very much in advance.
Comments
Hi Kiri,
Yes, the utf-8 can be a bit problematic. Basically, the program assumes by default that your text is ASCII encoded, even if the text itself was coded in utf-8. In order to prevent OpenSesame from getting confused, you want to add something like "stimulus = stimulus.decode('utf-8')", before you call the stimulus variable in a canvas.text command.
Hope this helps!
Cheers,
Josh
Hi Kiri,
In general, Josh is right that it's best to use
unicodestrings if your text contains special characters. If you decodestrtounicodeas soon as possible (e.g. directly after reading from a file) you usually avoid any encoding/ decoding trouble (which is troublesome in Python 2).However, this general guideline doesn't appear to be directly related to your problem. It seems that your text is actually numeric, that is, an
intobject. Possibly, this results from OpenSesame's 'smart typing', as described here:Could you post the full traceback from the debug window + a more detailed description of your experiment + your version of OpenSesame and operating system?
Cheers,
Sebastiaan
Check out SigmundAI.eu for our OpenSesame AI assistant!
Hi Josh and sebastiaan,
thank you for the quick answers. Tomorrow I will have the possibility to test your solutions and post the debug window as well as the versions of OpenSesame. The operating system is Windows 7.
In the experiment, some sentences will be presented in russian. To know which sentence is which one, they got integer codes in other cells of the csv file within the line. At first, the lines are read, then partet into the cells. During this progress, I printed the content into the debug window. Everything works fine there, and is presented as it should be.
But in Run-Phase, when I design the rows by the title-line, as soon as the first sentence should be presented the error message appears.
The version of OpenSesame is 2.9.x (current).
I tested it so far.
The first message of the debug window is:
Decoding the fields of the csv-file right after reading it didn't change this error message.
So what's in the inline script? Around line 68 you appear to be decoding the
int.Check out SigmundAI.eu for our OpenSesame AI assistant!
That's exactly the problem. Within the experiment, neither in Prepare nor in Run-Phase are integers declared in line 68. They are decoded in Prepare around line 60.
Here the actual decoding code (from line 52 on):
The error message says that
decode()is being called on anintobject. Python is usually not wrong about these things, so the question is where thisintcomes from. I would start by adding proper debugprintstatements, for example, like so:This way you can walk through all rows and fields to see if there's an
intin there. Then, once you know that's the case (or not), you can go back to whererowis created, and see what's going on.Cheers,
Sebastiaan
Check out SigmundAI.eu for our OpenSesame AI assistant!
Thank you very much, Sebastiaan.
It seems that one of the condition-coding rows can't be decoded. Yet, another row with similar input (integers, too) doesn't provoke any errors.
Here's the new error-message:
How do I change the type of the row into another variable-type? I already tried to change it into string by using
str()(asstr(row[field])among others) but this didn't work. Codec of the csv-file still says "UTF-8 w/o BOM".The file is read by the code as follows:
I think the easiest solution for you is to use the
UnicodeReader, which is a recipe described on the Python docs:You can add this snippet in a
initscript at the beginning of the experiment:Then you can read the csv file as before, except using
UnicodeReaderinstead ofcsv.DictReader. Like so:Make sure you don't mix
unicodeandstr! In other words, useu'unicode strings'instead of'str strings'when typing string literals in your code.Cheers,
Sebastiaan
Check out SigmundAI.eu for our OpenSesame AI assistant!
Thank you. I tested it but maybe I understood something wrong...
I filled in the code above (imports and class definitions) and afterwards let the script read the file as follows:
But now the script seems to declare
stringto every field of the csv-file. Therefore, I get the following error message:Error while executing inline script
phase: prepare
item: exp
line: 85
exception message: list indices must be integers, not unicode
exception type: TypeError
Traceback (also in debug window):
File "dist\libopensesame\inline_script.py", line 133, in prepare
File "", line 85, in
TypeError: list indices must be integers, not unicode
Neither in line 133 of prepare (where one of the conditions is assigned to a number for the trigger-function) nor in line 85 (which is the line right after printing the fields within the above code) seems to be a plausible reason for this error-message.
Within debug-window, the code is not printed out as letters, but as a row of character codes. (I think this is because of the print command using
u', but deleting it or replacing it withstr()doesn't work.)Might there be a way to assign
integerto specific fields of the csv-file?