代码之家  ›  专栏  ›  技术社区  ›  ceth

UnicodeDecodeError:“ascii”编解码器无法解码位置0中的字节0xe0:序号不在范围(128)内

  •  34
  • ceth  · 技术社区  · 14 年前

    在我的一台机器上,我在使用google apps引擎或django时出错。

    例如:

    • 应用程序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(不带gae)中处理静态文件时,我有非常相似的错误(堆栈不同)。

    我试图找到错误原因,并向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/I有五个带有俄语(西里尔语)字母的键。但我怎样才能纠正这个错误呢?

    5 回复  |  直到 14 年前
        1
  •  75
  •   bobince    14 年前

    这是个窃听器 mimetypes ,由注册表中的错误数据触发。( рєфшю/AMR 完全不是有效的MIME媒体类型。)

    ctype 是由返回的注册表项名称 _winreg.EnumKey ,其中 类群 应该是Unicode字符串,但不是。不像 _winreg.QueryValueEx , EnumKey 返回字节字符串(直接来自Windows API的ANSI版本; _winreg 在Python 2中,即使返回Unicode字符串,也不使用Unicode接口,因此它永远无法正确读取非ANSI字符)。

    所以试图 .encode 它在使用Unicode时失败 解码 –在将Unicode字符串编码回ASCII之前,尝试获取该字符串时出错!

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

    这些线在 类群 只需移除即可。

    预计到达时间: added to bug tracker .

        2
  •  8
  •   newtover    12 年前

    顺便说一下,问题的主要原因是QuickTime,它将非ascii mime类型添加到windows注册表中。最简单的修复方法是手动查找并从注册表中删除 HKCR/Mime/Database/ContentType/ аудио/ видео/ .

        3
  •  5
  •   Wangsu    11 年前

    有一个补丁:

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

    对我很有用。

    把UnicodeError换成UnicodeError

        4
  •  4
  •   HaveF    11 年前

    另一种解决方案 python issue9291 by Alexandr Zarubkin (me21) :

    在Lib\site packages文件夹中添加名为sitecustimize.py的文件。

    import sys
    sys.setdefaultencoding("cp1251")
    
        5
  •  1
  •   Codeboy.ru    12 年前

    它是一个python bug,注册表中有拉丁MIME提示 启动regedit并检查非拉丁名的“HKEY_CLASSES_ROOT\MIME\Database\Content Type”。