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

2024-04-18 06:39:56 发布

您现在位置:Python中文网/ 问答频道 /正文

在我的一台机器上,我在使用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有五个带有俄语(西里尔语)字母的键。但我怎样才能纠正这个错误呢?


Tags: inpydevimporthtmlgooglelinefiles
3条回答

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

这是由注册表中的错误数据触发的mimetypes中的错误。(рєфшю/AMR根本不是有效的MIME媒体类型。)

ctype是由_winreg.EnumKey返回的注册表项名称,该名称mimetypes应为Unicode字符串,但它不是。与_winreg.QueryValueEx不同,EnumKey返回字节字符串(直接来自Windows API的ANSI版本;_winreg在Python2中,即使返回Unicode字符串,也不使用Unicode接口,所以它永远不会正确地读取非ANSI字符)。

因此,尝试.encode失败,原因是在将Unicode字符串编码回ASCII之前尝试获取Unicode字符串时出现Unicode解码错误!

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

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

预计到达时间:added to bug tracker

有一个补丁:

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

对我很有用。

把UnicodeError换成UnicodeError

相关问题 更多 >