Python UnicodeDecodeError 解决办法

背景

最近在学习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


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值