python输入UnicodeDecodeError: [英] python input UnicodeDecodeError:

查看:82
本文介绍了python输入UnicodeDecodeError:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

python 3.x

python 3.x

>>> a = input()
hope
>>> a
'hope'
>>> b = input()
håpe
>>> b
'håpe'
>>> c = input()

start typing hå... delete using backspace... and change to hope

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
UnicodeDecodeError: 'utf8' codec can't decode byte 0xc3 in position 1: invalid continuation byte
>>> 

这种情况并不可怕,我正在解决它,但是发现删除时字节弄乱了,这很奇怪.其他人有没有经历过?

The situation is not terrible, I am working around it, but find it strange that when deleting, the bytes get messed up. Has anyone else experienced this?

终端历史记录表明它以为我输入了h?ope

the terminal history shows that it thought that I entered h?ope

有什么想法吗?

在使用此脚本的脚本中,我确实导入了readline来提供命令行历史记录.

in the script that is using this, I do import readline to give command line history.

推荐答案

似乎退格键正在删除最后一个 byte 而不是最后一个 character .最终发生的事情是您键入:

It looks like backspace is deleting the last byte instead of the last character. What ends up happening is that you type:

68 c3 a5
h |  å

68 c3 6f 70 65
h |  |o |p |e

在UTF-8中,设置了第一个位(c3)的字节意味着下一个字节也必须也设置了其第一个位(请参见

In UTF-8, a byte with the first bit set (c3) means that the next byte must have its first bit set as well (see Wikipedia's description).

请确保您的终端仿真器和readline了解您正在使用UTF-8.

Make sure that your terminal emulator and readline understand you're using UTF-8.

这篇关于python输入UnicodeDecodeError:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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