UnicodeDecodeError : ‘ascii’ codec can’t decode byte 0xe0 in position 0: ordinal not in range(128)

在安装某个Python的Package(BeautifulSoup)的时候出现这个问题。
解决方案一:
将Python安装路径中的Lib文件夹中的mimetypes.py中read_windows_registry()的四条语句注释掉:

def read_windows_registry(self, strict=True):
	……
	def enum_types(mimedb):
		……
			#try:
				#ctype = ctype.encode(default_encoding) # omit in 3.x!
			#except UnicodeEncodeError:
				#pass

解决方案二:
打patch:http://bugs.python.org/file18143/9291.patch
其实这个patch也是将上述语句注释掉。
产生此问题的原因:
这个Python的一个bug,下面是详细解释。

The `enum_types` function in `MimeTypes.read_windows_registry` tries to `.encode` the results of `EnumKey`, assuming it to be a Unicode string.
However, `_winreg.EnumKey` in Python 2.x actually returns a byte string (straight from the ANSI version of the registry interface). Consequently, if there is a MIME type registered with a non-ASCII character in its name (invalid, but not unheard of), initialising `MimeTypes` will raise a `UnicodeDecodeError`. This is not caught (it is only expecting a `UnicodeEncodeError`), so it bombs out whatever module indirectly caused `mimetypes.init()` to be called.
This attempt to `.encode` the `ctype` should simply be removed.

参考:
[1] http://stackoverflow.com/questions/4237898/unicodedecodeerror-ascii-codec-cant-decode-byte-0xe0-in-position-0-ordinal
[2] http://bugs.python.org/issue10490

Leave a Reply