在 Raspberry Pi 上发布解码 utf-8

Issue decoding utf-8 on a Raspberry Pi

我在 raspberry pi 上解码 utf-8 时遇到问题...相同的代码在我的计算机上有效。但不适用于覆盆子。有什么线索吗?

在我的笔记本电脑上(预期输出):

Python 3.5.3 (default, Sep 27 2018, 17:25:39) 
[GCC 6.3.0 20170516] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> b'\xc3\xa9'.decode('utf-8')
'é'

在树莓派上(意外输出):

Python 3.5.4 (default, Sep  5 2017, 18:32:10) 
[GCC 7.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> b'\xc3\xa9'.decode('utf-8')
'�'

区别在于两个系统上的区域设置。您可以通过以下方式在 python 中查看:

>>> import locale
>>> locale.getpreferredencoding()
UTF-8

您的一个系统应该报告 UTF-8 编码,而另一个则不会(大概是 ISO-8859-1)。在 Linux 上,检查 locale 命令的输出以检查差异,然后调整区域设置以匹配(通过 localectlupdate-locale 或您的发行版提供的任何内容)。