Python UnicodeDecodeError 解决办法

来源:互联网 发布:云推广软件 编辑:程序博客网 时间:2024/04/29 10:00

背景

最近在学习django,今天在启动django admin的时候,发现admin 默认的css没有加载到,然后一直报这个错误,然后去Google。

开始以为是css路径的问题,就各种改settings下的路径设置,发现怎么改也不对,后来经过一上午的搜索,发现这是Python 2.x 的bug。

抛异常的具体信息如下:

File "D:\Program Files\Python 2.7\lib\mimetypes.py", line 249, in enum_types    ctype = ctype.encode(default_encoding) # omit in 3.x!UnicodeDecodeError: 'ascii' codec can't decode byte 0xbc in position 0: ordinal not in range(128)

解决办法:

根据提示打开 \Python 2.x\lib\mimetypes.py  

def enum_types(mimedb):    i = 0    while True:        try:            ctype = _winreg.EnumKey(mimedb, i)        except EnvironmentError:            break        #try:            #ctype = ctype.encode(default_encoding) # omit in 3.x!        #except UnicodeEncodeError:            #pass        else:            yield ctype        i += 1

如上,注释或者去掉其中的 

try:     ctype = ctype.encode(default_encoding) # omit in 3.x!except UnicodeEncodeError:      pass
然后重新运行django admin即可

参考自 http://stackoverflow.com/questions/4237898/unicodedecodeerror-ascii-codec-cant-decode-byte-0xe0-in-position-0-ordinal

Python 官方bug描述 http://bugs.python.org/issue10490


0 0