How can I solve UnicodeDecodeError in Django?

2019-02-21 07:09发布

I am getting this error in Django:

 UnicodeDecodeError at /category/list/

 'utf8' codec can't decode byte 0xf5 in position 7: invalid start byte

 Request Method:    GET
 Request URL: ...
 Django Version:    1.3.1
 Exception Type:    UnicodeDecodeError
 Exception Value:   

 'utf8' codec can't decode byte 0xf5 in position 7: invalid start byte

 Exception Location:                       /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/encoder.py in iterencode, line 264
 ...

I should save Turkish characters in the database. How can I fix this error?

3条回答
对你真心纯属浪费
2楼-- · 2019-02-21 07:42

A start-byte of 0xf5 would indicate the start of a 4-character UTF-8 encoding. One strong possibility is that the input isn't UTF-8 at all but ISO-8859-9, the Turkish ISO encoding. On that codepage 0xf5 is a lowercase o with tilde or õ.

查看更多
姐就是有狂的资本
3楼-- · 2019-02-21 07:45

http://www.fileformat.info/info/unicode/char/f5/index.htm

it is an o with a tilde

try

some_string.decode('latin1','replace')
查看更多
地球回转人心会变
4楼-- · 2019-02-21 07:46

Below code solved my problem. Thank you.

if isinstance(encObject, unicode):
                           myStr = encObject.encode('utf-8')   
查看更多
登录 后发表回答