简体   繁体   中英

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

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,

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM