python prt_prt pythonpython2.7中的UnicodeDecodeError: 'ascii' codec can't decode byte 0...

python2.7中的UnicodeDecodeError: 'ascii' codec can't decode byte 0,有需要的朋友可以参考下。

在学习写一个简单的http服务器中报的错。

Traceback (most recent call last):

File "httpservice.py", line 5, in

import SimpleHTTPServer

File "C:Python27libSimpleHTTPServer.py", line 27, in

class SimpleHTTPRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):

File "C:Python27libSimpleHTTPServer.py", line 208, in SimpleHTTPRequestHandler

mimetypes.init() # try to read system mime.types

File "C:Python27libmimetypes.py", line 358, in init

db.read_windows_registry()

File "C:Python27libmimetypes.py", line 258, in read_windows_registry

for subkeyname in enum_types(hkcr):

File "C:Python27libmimetypes.py", line 249, in enum_types

ctype = ctype.encode(default_encoding) # omit in 3.x!

UnicodeDecodeError: 'ascii' codec can't decode byte 0xb0 in position 1: ordinal not in range(128)

原因:

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

python27中的bug

This is a bug in mimetypes, triggered by bad data in the registry. (рєфшю/AMR is not at all a valid MIME media type.)

W WW.002pc .COM从prt pythonpython2.7中的UnicodeDecodeError: 'ascii' codec can't decode byte 0分析来看,对prt pythonpython2.7中的UnicodeDecodeError: 'ascii' codec can't decode byte 0的结果。

ctype is a registry key name returned by _winreg.EnumKey, whichmimetypes is expecting to be a Unicode string, but it isn't. Unlike _winreg.QueryValueEx, EnumKey returns a byte string (direct from the ANSI version of the Windows API;

_winreg in Python 2 doesn't use the Unicode interfaces even though it returns Unicode strings, so it'll never read non-ANSI characters correctly).

So the attempt to .encode it fails with a Unicode​Decode​Error trying to get a Unicode string before encoding it back to ASCII!

解决方法:

修改默认的编码方式

import sys

reload(sys)

sys.setdefaultencoding('gb18030')

更多:prt pythonpython2.7中的UnicodeDecodeError: 'ascii' codec can't decode byte 0

https://www.002pc.comhttps://www.002pc.com/python/1582.html

你可能感兴趣的python2.7,decode,codec,UnicodeDecodeError,byte,ascii

No alive nodes found in your cluster

0踩

0 赞

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值