在 Raspberry Pi 上问题解码 utf-8 [英] Issue decoding utf-8 on a Raspberry Pi

查看:40
本文介绍了在 Raspberry Pi 上问题解码 utf-8的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 raspberry pi 上解码 utf-8 时遇到问题...同样的代码在我的电脑上也能运行.但不适用于树莓派.有什么线索吗?

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

Python 3.5.3(默认,2018 年 9 月 27 日,17:25:39)[GCC 6.3.0 20170516] 在 linux 上输入帮助"、版权"、信用"或许可"以获取更多信息.>>>b'\xc3\xa9'.decode('utf-8')'é'

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

Python 3.5.4(默认,2017 年 9 月 5 日,18:32:10)[GCC 7.2.0] 在 Linux 上输入帮助"、版权"、信用"或许可"以获取更多信息.>>>b'\xc3\xa9'.decode('utf-8')' '

解决方案

不同之处在于两个系统上的区域设置.您可以通过以下方式在 python 中检查:

<预><代码>>>>导入语言环境>>>locale.getpreferredencoding()UTF-8

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

I'm having trouble decoding utf-8 on a raspberry pi... The same code works on my computer. But doesn't works on the raspberry. Any clue ?

On my laptop (expected output) :

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')
'é'

On a raspberrypi (unexpected output) :

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')
'�'

解决方案

The difference is the locale setting on both systems. You can check this in python via:

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

One of your systems should report UTF-8 encoding, the other not (presumably the ISO-8859-1 instead). On Linux, check the output of the locale command to check for differences, then adjust the locale settings to match (via localectl or update-locale or whatever your distro offers).

这篇关于在 Raspberry Pi 上问题解码 utf-8的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆