gpt4 book ai didi

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

转载 作者:IT老高 更新时间:2023-10-28 21:50:28 24 4
gpt4 key购买 nike

在我的一台机器上,当我使用 google 应用程序引擎或 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(没有 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/我有五个带有俄语(西里尔)字母的键。但是我该如何解决这个错误呢?

最佳答案

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

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

因此,尝试 .encode 失败并返回 Unicode​Decode​在将 Unicode 字符串编码回 ASCII 之前尝试获取 Unicode 字符串时出错!

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

mimetypes 中的这些行应该被删除。

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

关于python - UnicodeDecodeError : 'ascii' codec can't decode byte 0xe0 in position 0: ordinal not in range(128),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4237898/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com