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 UnicodeDecodeError 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 赞