Encoding errors when using Chinese characters in Python3

Encoding errors when using Chinese characters in Python3

by Hongchuan Liu -
Number of replies: 1

print('中国')
The following error occurred while running the above code in Coderunner:
***Run error***
Traceback (most recent call last):
  File "__tester__.python3", line 1, in <module>
    print('\u4e2d\u56fd')
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-1: ordinal not in range(128)
But the Python 3 shell on the jobe server runs normally.
The jobe server environment is CentOS 7, Apache 2.4.39, PHP7.3.5 and jobe 1.6.6. The default encoding for CentOS and Apache has been set to UTF-8.
sys.stdout.encoding  in Jobe  returns 'utf-8', but in Coderunner it returns 'ANSI_X3.4-1968'.
I think that's the reason. It's strange why it's not 'UTF-8' in coderunner.

My current solution is to create a PROTOTYPE, add code, and explicitly set the encoding of stdin, stdout, and stderr to 'UTF-8'.


In reply to Hongchuan Liu

Re: Encoding errors when using Chinese characters in Python3

by Richard Lobb -
It sounds like your Jobe server is missing the installation step "Setting the locale" - see here.