您的位置:首页 > 其它

UnicodeEncodeError: 'ascii' codec can't encode characters in position

2014-09-05 10:20 387 查看
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-5: ordinal not in range(128)

Python UnicodeEncodeError: 'ascii' codec can't encode character

Date: Nov. 6, 2008 | Modified: June 19, 2012 | Tags:
django,
error, python,
wide |

35 Comments
UnicodeEncodeError: 'ascii' codec can't encode character u'\xa1'
in position 0: ordinal not in range(128)


If you've ever gotten this error, Django's
smart_str
function might be able to help. I found this from James Bennett's article,
Unicode in the real world. He provides a very good explanation of Python's Unicode and bytestrings, their use in Django, and using Django's Unicode utilities for working with non-Unicode-friendly
Python libraries. Here are my notes from his article as it applies to the above error. Much of the wording is directly from James Bennett's article.

This error occurs when you pass a Unicode string containing non-English characters (Unicode characters beyond 128) to something that expects an ASCII bytestring. The default encoding for a Python bytestring is ASCII, "which handles exactly 128 (English)
characters". This is why trying to convert Unicode characters beyond 128 produces the error.

The good news is that you can encode Python bytestrings in other encodings besides ASCII. Django's
smart_str
function in the
django.utils.encoding
module, converts a Unicode string to a bytestring using a default encoding of UTF-8.

Here is an example using the built-in function,
str
:

a = u'\xa1'
print str(a) # this throws an exception


Results:

Traceback (most recent call last):
File "unicode_ex.py", line 3, in
print str(a) # this throws an exception
UnicodeEncodeError: 'ascii' codec can't encode character u'\xa1' in position 0: ordinal not in range(128)

Here is an example using
smart_str
:

from django.utils.encoding import smart_str, smart_unicode

a = u'\xa1'
print smart_str(a)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐