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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4237898/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 14:53:19  来源:igfitidea点击:

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

pythondjangogoogle-app-engine

提问by demas

On one of my machines I have the error when I am working with google apps engine or django.

在我的一台机器上,当我使用谷歌应用程序引擎或 django 时出现错误。

For example:

例如:

  • 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>
    
  • 应用程序.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>
    

error message:

错误信息:

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)

When I am working with static files in django (without gae) I have very similar error (with different stack).

当我在 django(没有 gae)中处理静态文件时,我有非常相似的错误(具有不同的堆栈)。

I tried to find the reason of error and added code to mimetypes.py:

我试图找到错误的原因,并在 mimetypes.py 中添加了代码:

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

Then I get next messages in my console:

然后我在控制台中收到下一条消息:

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

In the registry HKCR/Mime/Database/ContentType/ I have five keys with russian (cyrilic) letters. But how can I fix this error?

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

采纳答案by bobince

This is a bug in mimetypes, triggered by bad data in the registry. (р?фшю/AMRis not at all a valid MIME media type.)

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

ctypeis a registry key name returned by _winreg.EnumKey, which mimetypesis expecting to be a Unicode string, but it isn't. Unlike _winreg.QueryValueEx, EnumKeyreturns a byte string (direct from the ANSI version of the Windows API; _winregin Python 2 doesn't use the Unicode interfaces even though it returns Unicode strings, so it'll never read non-ANSI characters correctly).

ctype是由 返回的注册表项名称_winreg.EnumKey,它mimetypes期望是一个 Unicode 字符串,但它不是。与_winreg.QueryValueEx,不同,EnumKey返回一个字节字符串(直接来自 Windows API 的 ANSI 版本;_winreg在 Python 2 中,即使它返回 Unicode 字符串也不使用 Unicode 接口,因此它永远不会正确读取非 ANSI 字符)。

So the attempt to .encodeit fails with a Unicode?Decode?Error trying to get a Unicode string before encoding it back to ASCII!

那么尝试.encode使用 Unicode 失败了?解码?在将其编码回 ASCII 之前尝试获取 Unicode 字符串时出错!

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

These lines in mimetypesshould simply be removed.

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

ETA: added to bug tracker.

ETA:添加到错误跟踪器

回答by Codeboy.ru

its a python bug with latin MIME tipes in registry start regedit and inspect "HKEY_CLASSES_ROOT\MIME\Database\Content Type" for non-latin names.

它是在注册表中带有拉丁文 MIME 提示的 python 错误,启动 regedit 并检查“HKEY_CLASSES_ROOT\MIME\Database\Content Type”以获取非拉丁文名称。

回答by newtover

By the way, the main culpit of the problem is QuickTime which adds non-ascii mime types to the windows registry. The easiest way to fix it is to manually find and remove from the registry the subsections of the HKCR/Mime/Database/ContentType/starting with аудио/and видео/.

顺便说一下,问题的罪魁祸首是 QuickTime,它向 Windows 注册表添加了非 ascii mime 类型。修复它的最简单方法是手动查找并从注册表中删除HKCR/Mime/Database/ContentType/аудио/和开头的小节видео/

回答by Wangsu

There is a patch:

有一个补丁:

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

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

Works great for me.

对我很有用。

Just replace UnicodeEncodeError to be UnicodeError

只需将 UnicodeEncodeError 替换为 UnicodeError

回答by HaveF

An alternative solution from python issue9291 by Alexandr Zarubkin (me21):

来自Alexandr Zarubkin (me21) 的 python issue9291的替代解决方案:

add file named sitecustomize.py in Lib\site-packages folder.

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

import sys
sys.setdefaultencoding("cp1251")