I’m not sure if this is properly a Pi related question, but it came to light during my tinkering with an ARM build, so…
On the Pi, I have a Python script that does some data gathering for me, then formats it nicely and dumps it to a terminal. There are temperatures involved, so the output contains degree symbols. The text in Python is explicitly declared to be unicode.
When this script is run from a terminal window on my Mac or on the Pi itself, the output is as expected. If I attempt to invoke it from a shell object in a Xojo app on the pi, Python throws an exception about not being able to convert certain unicode characters to ASCII (specifically the degree symbol.)
Does the shell class only deal with ASCII? Is there a way to make it use unicode?
The Shell class isn’t the same thing as opening a terminal window - it does not have the same environment variables, etc. Hope that starts you in the right direction.
Should have thought of that. Thanks. Unfortunately, it doesn’t seem to be as simple as setting LANG. This has no effect. It might be some nerdy Python thing I’m unaware of. Will post here if I ever solve it. For now, I’m just trapping the exception with a Try block, and in that case, converting output to ASCII. Unsatisfying, but functional.
Well, that was quick. There is this environment variable;
PYTHONIOENCODING=UTF-8
Setting it using System.EnvironmentVariable( “PYTHONIOENCODING” ) = “UTF-8” did nothing, but
prepending it to the actual command string I sent to the terminal solved the problem.
Weird, but I’ll take it.
And as an addendum, it seems this applies only to Python < 3, and is considered a dangerous hack for various reasons. The solution I’ve adopted is as follows. In my Python 2.7 script;
[code]# ----- Hack for printing UTF-8 --------------------------------------
|
import sys
reload(sys)
def safePrint( txt ):
enc = sys.getdefaultencoding()
sys.setdefaultencoding(‘utf-8’)
print txt
sys.setdefaultencoding( enc )
|
--------------------------------------------------------------------
[/code]
I then use safePrint() in place of print, and all seems to work well. Xojo’s shell gets its UTF-8 strings back from the Python script, and the danger to Python should be minimal. I’m sure there are likely drawbacks to this as well, but I haven’t found them yet, and it works well enough.
Not sure if anyone will ever need this besides me, but here it is for posterity.