Allan Vestergaard
Allan Vestergaard

Reputation: 1

Simple print statement gives UnicodeEncodeError on one of two RPI

When I copied my program from one RPI to another I suddenly got a lot of errors in the log file. I have narrowed the problem down to a thing related to danish letters ÆØÅ.

#!/usr/bin/env python3
# -*- coding: utf-8 -*-    
print("ABC æøå ÆØÅ")  #Danish characters

On One of my RasberryPies it gives this error.

Traceback (most recent call last):File "test.py", line 5, in <module>
print("ABC \xe6\xf8\xe5 \xc6\xd8\xc5")
UnicodeEncodeError: 'ascii' codec can't encode characters in position 4-6: ordinal not in range(128)

On the two others i runs perfectly.

Python version Python 3.5.3 Checked danish localization with raspi-config.

-*- coding is the correct way

Upvotes: 0

Views: 445

Answers (1)

Kushan Gunasekera
Kushan Gunasekera

Reputation: 8586

Before execute python command, add below lines to your terminal.

export PYTHONIOENCODING=utf-8

Now run python test.py in the same terminal.

Or you can add below lines in top of your test.py file

import sys
sys.setdefaultencoding('utf-8')

For more details, please check following question,

Upvotes: 1

Related Questions