简体   繁体   English

简单的打印语句在两个RPI之一上给出UnicodeEncodeError

[英]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. 当我将程序从一个RPI复制到另一个RPI时,突然在日志文件中出现了很多错误。 I have narrowed the problem down to a thing related to danish letters ÆØÅ. 我把问题缩小到与丹麦字母lettersØÅ有关的问题。

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

On One of my RasberryPies it gives this error. 在我的一个RasberryPies上,出现此错误。

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. Python版本Python 3.5.3使用raspi-config检查了丹麦语的本地化。

-*- coding is the correct way -*-编码是正确的方法

Before execute python command, add below lines to your terminal. 在执行python命令之前,将以下几行添加到您的终端。

export PYTHONIOENCODING=utf-8

Now run python test.py in the same terminal. 现在在同一终端上运行python test.py

Or you can add below lines in top of your test.py file 或者,您可以在test.py文件顶部添加以下行

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

For more details, please check following question, 有关更多详细信息,请检查以下问题,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 return语句中的Lambda提供两个输出,但是如果不使用print则只有一个输出 - Lambda in return statement gives two outputs but only one if print is not used 加入两个for循环,由print语句分隔成一个循环 - Join two for loops, separated by a print statement into one Python:RPI上的简单UDP服务器不会打印消息 - Python: simple UDP server on RPI does not print message 一个打印语句将两个变量打印在两条不同的行上 - One print statement two variables get printed on two different lines Python:当我有一个if语句和一个分配给3个整数的打印件时,我输入一个更高的值,它给出了这两个,并且打印正确 - Python: When I have a if statement and a print assigned to 3 integers, and I type one higher, it gives both that, and the correct print 如何修复打印语句,使其位于一行而不是两行? - How to fix the print statement so that it is in one line instead of two? 一份打印声明 - One single print statement Python 给出 UnicodeEncodeError 读取 xlsx - Python gives UnicodeEncodeError reading xlsx 在一条python行中使用一个简单的语句制作两个条件/循环语句 - Making two conditional/loop statements with a simple statement work in one python line 简单的打印语句不适用于python中的if语句的方法 - simple print statement is not working inside method with if statement in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM