Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python UnicodeDecodeError:&x27;ascii';编解码器可以';t解码位置0处的字节0xe0:序号不在范围内(128)_Python_Django_Google App Engine - Fatal编程技术网

Python UnicodeDecodeError:&x27;ascii';编解码器可以';t解码位置0处的字节0xe0:序号不在范围内(128)

Python UnicodeDecodeError:&x27;ascii';编解码器可以';t解码位置0处的字节0xe0:序号不在范围内(128),python,django,google-app-engine,Python,Django,Google App Engine,在我的一台机器上,当我使用google apps engine或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: /.* s

在我的一台机器上,当我使用google apps engine或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
    
  • Demas125C.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>
    
    然后我在控制台中收到下一条消息:

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

    在注册表HKCR/Mime/Database/ContentType/I中有五个带有俄语(西里尔语)字母的键。但是如何修复此错误?

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

    ctype
    是由
    \u winreg.EnumKey
    返回的注册表项名称,
    mimetypes
    应为Unicode字符串,但不是。与
    \u winreg.queryvaluex
    不同,
    EnumKey
    返回字节字符串(直接从Windows API的ANSI版本;Python 2中的
    \u winreg
    即使返回Unicode字符串也不使用Unicode接口,因此它永远无法正确读取非ANSI字符)

    因此,尝试
    .encode
    的Unicode编码失败​解码​在将Unicode字符串编码回ASCII之前尝试获取该字符串时出错

    try:
        ctype = ctype.encode(default_encoding) # omit in 3.x!
    except UnicodeEncodeError:
        pass
    
    只需删除
    mimetypes
    中的这些行即可


    ETA:。

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

    顺便说一句,问题的主要原因是QuickTime,它将非ascii MIME类型添加到windows注册表中。修复它的最简单方法是手动查找并从注册表中删除
    HKCR/Mime/Database/ContentType/
    的子部分,从
    ааааааааа/
    开始,有一个补丁:

    这对我很有用


    只需将UnicodeEncodeError替换为UnicodeError即可

    以下替代解决方案:

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

    import sys
    sys.setdefaultencoding("cp1251")
    

    “default_encoding”的值是多少,现在看来它是无法转换西里尔语的,在这里使用UTF-8可能会修复错误。ascii。我试图将其更改为utf-8,但出现错误“UnicodeDecodeError:'utf8'编解码器无法解码位置0中的字节0xef:无效的继续字节”。无论如何,我从注册表中删除了这些键,错误消失了。这是一个bug mimetype.py更改了此url的python代码谢谢,这修复了我在Anaconda v2.1上使用IPython的问题!被接受的答案奏效了,我没有尝试这个,但我认为它也会奏效。
    try:
        ctype = ctype.encode(default_encoding) # omit in 3.x!
    except UnicodeEncodeError:
        pass
    
    import sys
    sys.setdefaultencoding("cp1251")