Is it possible to specify encoding for one line only?

Hello,

I’m very new to both PsychoPy and this forum, so appologies if I’m asking a silly question!

I’m trying to run an existing script and to translate the experiment into my language (Czech). However, if I put non-ascii characters into the line that prints experiment instructions, I get the following error message:

File “D:\Documents\Studijni_texty\Bakalarka\Experiment\rcictask.py”, line 11
SyntaxError: Non-ASCII character ‘\xc3’ in file D:\Documents\Studijni_texty\Bakalarka\Experiment\rcictask.py on line 11, but no encoding declared; see PEP 263 – Defining Python Source Code Encodings | peps.python.org for details

I tried, therefore, to specify the encoding at the very beginning of the script as follows:

However, when running the script, I got the following message:

Traceback (most recent call last):
File “D:\Documents\Studijni_texty\Bakalarka\Experiment\rcictask.py”, line 14, in
infoDlg = gui.DlgFromDict(dictionary=info, title=‘rcicr demo’, order=[‘Instruction’, ‘Stimuli folder’, ‘Data file’])
File “C:\Program Files (x86)\PsychoPy2\lib\site-packages\psychopy-1.84.2-py2.7.egg\psychopy\gui\qtgui.py”, line 377, in init
self.addField(field, self.dictionary[field], tip=tooltip)
File “C:\Program Files (x86)\PsychoPy2\lib\site-packages\psychopy-1.84.2-py2.7.egg\psychopy\gui\qtgui.py”, line 174, in addField
inputBox = QtWidgets.QLineEdit(unicode(initial), parent=self)
UnicodeDecodeError: ‘ascii’ codec can’t decode byte 0xc3 in position 4: ordinal not in range(128)

As I understand it (correct me please, if I’m wrong), the script requires ascii encoding for line 14. Is there a way to change the encoding for specific commands? Or do you have any different recommendation?

Thanks!

Anna

Hello Anna, no unfortunately it’s not that simple, and you actually can’t use ASCII if you want Czech characters. Your best bet is to use utf-8. The shebang you put at the top of the file allows for utf-8 IN THE CODE FILE, which is why your first error was fixed, but it doesn’t affect the encoding of input and output files, so it’s not a cure all.

We can try to track the source of the error if you include some files for us to mess with. But in the meantime, one thing you can start doing, and which might fix your problem, is to look for any strings in your code file (that is, words between single or double-quotes), and put a u in front of them to make them Unicode objects, like this:

#not good, will break
bad = 'çõøk'
# better, hopefully won't break
good = u'çõøk'

Try that out and hopefully your problem will be resolved.

2 Likes

If you want to understand a little more about encodings, since it’s a very good thing to know the basics, this article is a good one:

http://kunststube.net/encoding/

You might not understand everything on the first read, but it’s worth knowing.

2 Likes

Thanks, it indeed helps to put a “u” in front of the strings (when the encoding is specified at the beginning). :slight_smile: