Unicode解码错误:'ascii'编解码器无法解码字节0xe0,位置0:序数不在范围(128)内

34 投票
5 回答
45395 浏览
提问于 2025-04-16 07:24

在我的一台电脑上,当我在使用谷歌应用引擎或者Django时,遇到了一个错误。

举个例子:

  • app.yaml

    application: demas1252c
    version: 1
    runtime: python
    api_version: 1
    
    
    handlers:
       - url: /images
    static_dir: images
       - url: /css
    static_dir: css
       - url: /js
    static_dir: js
       - url: /.*
    script: demas1252c.py
    
  • demas1252c.py

    import cgi
    import wsgiref.handlers
    
    
    from google.appengine.ext.webapp import template
    from google.appengine.ext import webapp
    
    
    class MainPage(webapp.RequestHandler): 
    def get(self):
    values = {'id' : 10}
    
    
    self.response.out.write(template.render('foto.html', values))
    
    
    application = webapp.WSGIApplication([('/', MainPage)], debug = True)
    wsgiref.handlers.CGIHandler().run(application)
    
  • foto.html

    <!DOCTYPE html>
    <html lang="en">
        <head></head>
    <body>some</body>
    </html>
    

错误信息是:

C:\artefacts\dev\project>"c:\Program Files\Google\google_appengine\dev_appserver.py" foto-hosting
Traceback (most recent call last):
  File "c:\Program Files\Google\google_appengine\dev_appserver.py", line 69, in <module>
    run_file(__file__, globals())
  File "c:\Program Files\Google\google_appengine\dev_appserver.py", line 65, in run_file
    execfile(script_path, globals_)
  File "c:\Program Files\Google\google_appengine\google\appengine\tools\dev_appserver_main.py", line 92, in <module>
    from google.appengine.tools import dev_appserver
  File "c:\Program Files\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 140, in <module>
    mimetypes.add_type(mime_type, '.' + ext)
  File "C:\Python27\lib\mimetypes.py", line 344, in add_type
    init()
  File "C:\Python27\lib\mimetypes.py", line 355, in init
    db.read_windows_registry()
  File "C:\Python27\lib\mimetypes.py", line 260, in read_windows_registry
    for ctype in enum_types(mimedb):
  File "C:\Python27\lib\mimetypes.py", line 250, in enum_types
    ctype = ctype.encode(default_encoding) # omit in 3.x!
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe0 in position 0: ordinal not in range(128)

当我在Django中处理静态文件(不使用谷歌应用引擎)时,也遇到了类似的错误(只是堆栈信息不同)。

我尝试找出错误的原因,并在mimetypes.py文件中添加了一些代码:

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

然后我在控制台中得到了以下信息:

=====
video/x-ms-wvx
=====
video/x-msvideo
=====
рєфшю/AMR
Traceback (most recent call last):

在注册表HKCR/Mime/Database/ContentType/中,我有五个带有俄文(西里尔字母)的键。可是我该怎么解决这个错误呢?

5 个回答

5

这里有一个补丁:

http://bugs.python.org/file18143/9291.patch

对我来说效果很好。

只需要把 UnicodeEncodeError 替换成 UnicodeError 就可以了。

8

顺便说一下,问题的主要原因是QuickTime,它在Windows注册表中添加了非ASCII的MIME类型。解决这个问题最简单的方法就是手动去注册表中找到并删除以аудио/видео/开头的HKCR/Mime/Database/ContentType/的子项。

75

这是一个在 mimetypes 中的错误,是因为注册表里的数据有问题引起的。(рєфшю/AMR 根本就不是一个有效的 MIME 媒体类型。)

ctype 是通过 _winreg.EnumKey 返回的一个注册表键名,mimetypes 期望它是一个 Unicode 字符串,但实际上并不是。和 _winreg.QueryValueEx 不同,EnumKey 返回的是字节字符串(直接来自 Windows API 的 ANSI 版本;在 Python 2 中,_winreg 不使用 Unicode 接口,尽管它返回的是 Unicode 字符串,所以它永远无法正确读取非 ANSI 字符)。

因此,尝试对它使用 .encode 时,会因为在编码成 ASCII 之前想要获取 Unicode 字符串而失败,出现了 Unicode解码错误!

try:
    ctype = ctype.encode(default_encoding) # omit in 3.x!
except UnicodeEncodeError:
    pass

mimetypes 中的这些代码行应该简单地删除。

补充说明:已添加到错误追踪器

撰写回答