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

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

问题描述

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

I'm simply trying to decode \uXXXX\uXXXX\uXXXX-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'\u041e\u043b\u044c\u0433\u0430'.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)

我是Python新手.怎么了谢谢!

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

推荐答案

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

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.

如果您具有Unicode数据,则仅将编码编码为UTF-8是有意义的,而不是解码:

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

>>> print u'\u041e\u043b\u044c\u0433\u0430'
Ольга
>>> u'\u041e\u043b\u044c\u0433\u0430'.encode('utf8')
'\xd0\x9e\xd0\xbb\xd1\x8c\xd0\xb3\xd0\xb0'

如果需要Unicode值,则只需使用Unicode文字(u'...').无需进一步解码.

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'\u041e\u043b\u044c\u0433\u0430'.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天全站免登陆