Django.utils.编码.djangunicodededecodee公司

2024-05-28 20:38:39 发布

您现在位置:Python中文网/ 问答频道 /正文

当我试图通过泛型关系向Django模型添加一个条目时,出现了以下错误。在

django.utils.encoding.DjangoUnicodeDecodeError: 'utf8' codec can't decode byte 0xb8 in position 24: unexpected code byte. You passed in 'ASL/60Styles_Timeless-3_\xb8 CaLe.asl' (<type 'str'>)

模型如下:

^{pr2}$

表的字符集是utf8,排序规则是utf8_general_ci。在

这是否意味着文件名不是有效的utf8字符串?如何修复此错误,或者我们可以将无效字符串转换为有效格式?在


Tags: django字符串in模型关系错误条目utils
1条回答
网友
1楼 · 发布于 2024-05-28 20:38:39

您的文件系统显然没有使用UTF-8编码:

>>> a = 'ASL/60Styles_Timeless-3_\xb8 CaLe.asl'
>>> print a.decode('utf-8')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.5/encodings/utf_8.py", line 16, in decode
    return codecs.utf_8_decode(input, errors, True)
UnicodeDecodeError: 'utf8' codec can't decode byte 0xb8 in position 24: unexpected code byte
>>> a.decode('iso8859-2')
u'ASL/60Styles_Timeless-3_\xb8 CaLe.asl'
>>> print a.decode('iso8859-2')
ASL/60Styles_Timeless-3_¸ CaLe.asl

直到现在我才意识到你得到的字符串实际上已经是unicode了。尝试使用此命令获取unicode:

^{pr2}$

相关问题 更多 >

    热门问题