1

One of the great improvements of Python 3 over Python 2 is being able to work with non-ascii text, but it doesn't seem to be working on the pi :(

On my pc (python 3.4.1) this works as expected, printing 你好 to the console

print('你好')

but on my pi (python 3.6.1) I get this error:

Traceback (most recent call last):
  File "test.py", line 1, in <module>
    print('\u4f60\u597d')
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-1: ordinal not in range(128)

Is there some configuration I'm missing? I don't think it's the terminal setting since the command line echo "你好" works.

If it makes any difference, I'm running Arch linux on the pi.

  • I can't actually test it, but did you try : print(u'你好') ? – Technico.top Jun 23 '17 at 10:32
  • same result - I think the 'u' is ignored in python 3 as all strings are unicode – Rob Agar Jun 23 '17 at 10:37
  • 1
    If you're running this in a terminal window, is the terminal window set up for unicode? I get the same or a very similar error running ubuntu on my chromebook if not. – Chris H Jun 23 '17 at 10:56
2

Ah fixed it - just needed to set the OS locale to en-GB.UTF8 and log back in.

  • You can also set the preferences for your terminal specifically. In xfce4-terminal, for example, the "Terminal" menu allows you to set it for the current window, or Edit->Preferences if you want it to stick – Chris H Jun 23 '17 at 11:00
  • 1
    thanks, but as this is running headless I had to set the system default locale – Rob Agar Jun 23 '17 at 11:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.