UnicodeEncodeError: 'ascii' 编解码器无法对位置 0-5 中的字符进行编码:序号不在范围内 (128) [英] UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-5: ordinal not in range(128)

查看:20
本文介绍了UnicodeEncodeError: 'ascii' 编解码器无法对位置 0-5 中的字符进行编码:序号不在范围内 (128)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想解码类似 uXXXXuXXXXuXXXX 的字符串.但我收到一个错误:

$ pythonPython 2.7.6(默认,2014 年 9 月 9 日,15:04:36)[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.39)] on darwin输入帮助"、版权"、信用"或许可证"以获取更多信息.>>>打印 u'u041eu043bu044cu0433u0430'.decode('utf-8')回溯(最近一次调用最后一次):文件<stdin>",第 1 行,在 <module> 中文件/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/encodings/utf_8.py",第 16 行,解码返回 codecs.utf_8_decode(输入,错误,真)UnicodeEncodeError: 'ascii' 编解码器无法对位置 0-4 中的字符进行编码:序号不在范围内 (128)

我是 Python 新手.有什么问题?谢谢!

解决方案

Python 正在努力提供帮助.您无法解码 Unicode 数据,它已经被解码了.因此,Python 首先将编码数据(使用 ASCII 编解码器)以获取要解码的字节.正是这种隐式编码失败了.

如果您有 Unicode 数据,则只有将编码到 UTF-8 才有意义,而不是解码:

<预><代码>>>>打印 u'u041eu043bu044cu0433u0430'Ольга>>>u'u041eu043bu044cu0433u0430'.encode('utf8')'xd0x9exd0xbbxd1x8cxd0xb3xd0xb0'

如果您想要一个 Unicode 值,那么您只需要使用 Unicode 文字 (u'...').无需进一步解码.

同样的隐式转换发生在另一个方向;如果您尝试对字节串进行编码,则会触发隐式解码:

<预><代码>>>>u'u041eu043bu044cu0433u0430'.encode('utf8').encode('utf8')回溯(最近一次调用最后一次):文件<stdin>",第 1 行,在 <module> 中UnicodeDecodeError: 'ascii' 编解码器无法解码位置 0 中的字节 0xd0:序号不在范围内 (128)

I'm simply trying to decode uXXXXuXXXXuXXXX-like string. But I get an error:

$ python
Python 2.7.6 (default, Sep  9 2014, 15:04:36) 
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.39)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> print u'u041eu043bu044cu0433u0430'.decode('utf-8')
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/encodings/utf_8.py", line 16, in decode
    return codecs.utf_8_decode(input, errors, True)

    UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-4: ordinal not in range(128)

I'm Python newbie. What's a problem? Thanks!

解决方案

Python is trying to be helpful. You cannot decode Unicode data, it is already decoded. So Python first will encode the data (using the ASCII codec) to get bytes to decode. It is this implicit encoding that fails.

If you have Unicode data, it only makes sense to encode to UTF-8, not decode:

>>> print u'u041eu043bu044cu0433u0430'
Ольга
>>> u'u041eu043bu044cu0433u0430'.encode('utf8')
'xd0x9exd0xbbxd1x8cxd0xb3xd0xb0'

If you wanted a Unicode value, then using a Unicode literal (u'...') is all you needed to do. No further decoding is necessary.

The same implicit conversion takes place in the other direction; if you tried to encode a bytestring you'd trigger an implicit decoding:

>>> u'u041eu043bu044cu0433u0430'.encode('utf8').encode('utf8')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
UnicodeDecodeError: 'ascii' codec can't decode byte 0xd0 in position 0: ordinal not in range(128)

这篇关于UnicodeEncodeError: 'ascii' 编解码器无法对位置 0-5 中的字符进行编码:序号不在范围内 (128)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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