SQLAlchemy - 如何避免错误:UnicodeEncodeError: 'latin-1' 编解码器无法编码字符

2 投票
1 回答
3784 浏览
提问于 2025-04-17 10:24

这是模型的列:

title = Column(Unicode(100))

当我尝试在 title 中添加包含西里尔字母的记录时,出现了一个错误:

UnicodeEncodeError: 'latin-1' 编码无法编码位置上的字符...

如果我用 utf-8 编码它:

title = request.POST['title'].encode('utf-8')

我得到了下一个错误:

ProgrammingError: 除非使用可以解释 8 位字节字符串的 text_factory(比如 text_factory = str),否则不能使用 8 位字节字符串。强烈建议你将应用程序切换到 Unicode 字符串。

当我解码 utf-8 时:

title = request.POST['title'].decode('utf-8')

又出现了一个新错误:

UnicodeDecodeError: 'ascii' 编码无法编码字符...

我该怎么办?

编辑:

我正在使用 Sqlite3,我觉得这可能很重要。

编辑 2:(代码和追踪信息)

我的代码:

title = request.POST['title']

new_model = Model(
    ...
    title = title,
    ...
)
DBSession.add(new_model)
DBSession.flush()

追踪信息(最近的调用在最前面):

文件 "/home/p/work/SUN/lib/python2.6/site-packages/pyramid_debugtoolbar-0.9.7-py2.6.egg/pyramid_debugtoolbar/panels/performance.py",第 55 行,在 resource_timer_handler 中

result = handler(request)

文件 "/home/p/work/SUN/lib/python2.6/site-packages/pyramid-1.3a3-py2.6.egg/pyramid/tweens.py",第 20 行,在 excview_tween 中

response = handler(request)

文件 "/home/p/work/SUN/lib/python2.6/site-packages/pyramid_tm-0.3-py2.6.egg/pyramid_tm/init.py",第 61 行,在 tm_tween 中

response = handler(request)

文件 "/home/p/work/SUN/lib/python2.6/site-packages/pyramid-1.3a3-py2.6.egg/pyramid/router.py",第 164 行,在 handle_request 中

response = view_callable(context, request)

文件 "/home/p/work/SUN/lib/python2.6/site-packages/pyramid-1.3a3-py2.6.egg/pyramid/config/views.py",第 316 行,在 rendered_view 中

result = view(context, request)

文件 "/home/p/work/SUN/lib/python2.6/site-packages/pyramid-1.3a3-py2.6.egg/pyramid/config/views.py",第 426 行,在 _requestonly_view 中

response = view(request)

文件 "/home/p/work/SUN/sunviver/sunviver/views/advert.py",第 187 行,在 create 中

return HTTPFound(location=new_advert.url())

文件 "/home/p/work/SUN/lib/python2.6/site-packages/pyramid-1.3a3-py2.6.egg/pyramid/httpexceptions.py",第 444 行,在 init

body_template=body_template, location=location, **kw)

文件 "/home/p/work/SUN/lib/python2.6/site-packages/pyramid-1.3a3-py2.6.egg/pyramid/httpexceptions.py",第 213 行,在 init

Response.init(self, status=status, **kw)

文件 "build/bdist.linux-i686/egg/webob/response.py",第 141 行,在 init

setattr(self, name, value)

文件 "build/bdist.linux-i686/egg/webob/descriptors.py",第 112 行,在 fset 中

value = value.encode('latin-1')

还有 new_advert.url()

def url(self):
    return '/%s/%s' % (self.id, self.title.replace(' ', '_'))

@J.F. Sebastian

追踪信息(最近的调用在最前面): 文件 "/usr/lib/python2.6/wsgiref/handlers.py",第 93 行,在 run 中

self.result = application(self.environ, self.start_response)

文件 "/home/ponomar/v/ENV/lib/python2.6/site-packages/pyramid-1.3a3-py2.6.egg/pyramid/router.py",第 187 行,在 call

response = self.handle_request(request)

文件 "/home/ponomar/v/ENV/lib/python2.6/site-packages/pyramid_debugtoolbar-0.9.7-py2.6.egg/pyramid_debugtoolbar/toolbar.py",第 157 行,在 toolbar_tween 中

toolbar.process_response(response)

文件 "/home/ponomar/v/ENV/lib/python2.6/site-packages/pyramid_debugtoolbar-0.9.7-py2.6.egg/pyramid_debugtoolbar/toolbar.py",第 55 行,在 process_response 中

vars, request=request)

文件 "/home/ponomar/v/ENV/lib/python2.6/site-packages/pyramid-1.3a3-py2.6.egg/pyramid/renderers.py",第 81 行,在 render 中

return helper.render(value, None, request=request)

文件 "/home/ponomar/v/ENV/lib/python2.6/site-packages/pyramid-1.3a3-py2.6.egg/pyramid/renderers.py",第 420 行,在 render 中

result = renderer(value, system_values)

文件 "/home/ponomar/v/ENV/lib/python2.6/site-packages/pyramid-1.3a3-py2.6.egg/pyramid/mako_templating.py",第 162 行,在 call

reraise(MakoRenderingException(errtext), None, exc_info[2])

文件 "/home/ponomar/v/ENV/lib/python2.6/site-packages/pyramid-1.3a3-py2.6.egg/pyramid/mako_templating.py",第 154 行,在 call

result = template.render_unicode(**system)

文件 "/home/ponomar/v/ENV/lib/python2.6/site-packages/Mako-0.5.0-py2.6.egg/mako/template.py",第 311 行,在 render_unicode 中

as_unicode=True)

文件 "/home/ponomar/v/ENV/lib/python2.6/site-packages/Mako-0.5.0-py2.6.egg/mako/runtime.py",第 660 行,在 _render 中

**_kwargs_for_callable(callable_, data))

文件 "/home/ponomar/v/ENV/lib/python2.6/site-packages/Mako-0.5.0-py2.6.egg/mako/runtime.py",第 692 行,在 _render_context 中

_exec_template(inherit, lclcontext, args=args, kwargs=kwargs)

文件 "/home/ponomar/v/ENV/lib/python2.6/site-packages/Mako-0.5.0-py2.6.egg/mako/runtime.py",第 718 行,在 _exec_template 中

callable_(context, *args, **kwargs)

文件 "pyramid_debugtoolbar_templates_toolbar_mako",第 117 行,在 render_body 中

文件 "/home/ponomar/v/ENV/lib/python2.6/site-packages/pyramid_debugtoolbar-0.9.7-py2.6.egg/pyramid_debugtoolbar/panels/headers.py",第 54 行,在 content 中

vars, self.request)

文件 "/home/ponomar/v/ENV/lib/python2.6/site-packages/pyramid_debugtoolbar-0.9.7-py2.6.egg/pyramid_debugtoolbar/panels/init.py",第 24 行,在 render 中

return render(template_name, vars, request=request)

文件 "/home/ponomar/v/ENV/lib/python2.6/site-packages/pyramid-1.3a3-py2.6.egg/pyramid/renderers.py",第 81 行,在 render 中

return helper.render(value, None, request=request)

文件 "/home/ponomar/v/ENV/lib/python2.6/site-packages/pyramid-1.3a3-py2.6.egg/pyramid/renderers.py",第 420 行,在 render 中

result = renderer(value, system_values)

文件 "/home/ponomar/v/ENV/lib/python2.6/site-packages/pyramid-1.3a3-py2.6.egg/pyramid/mako_templating.py",第 162 行,在 call

reraise(MakoRenderingException(errtext), None, exc_info[2])

文件 "/home/ponomar/v/ENV/lib/python2.6/site-packages/pyramid-1.3a3-py2.6.egg/pyramid/mako_templating.py",第 154 行,在 call

result = template.render_unicode(**system)

文件 "/home/ponomar/v/ENV/lib/python2.6/site-packages/Mako-0.5.0-py2.6.egg/mako/template.py",第 311 行,在 render_unicode 中

as_unicode=True)

文件 "/home/ponomar/v/ENV/lib/python2.6/site-packages/Mako-0.5.0-py2.6.egg/mako/runtime.py",第 660 行,在 _render 中

**_kwargs_for_callable(callable_, data))

文件 "/home/ponomar/v/ENV/lib/python2.6/site-packages/Mako-0.5.0-py2.6.egg/mako/runtime.py",第 692 行,在 _render_context 中

_exec_template(inherit, lclcontext, args=args, kwargs=kwargs)

文件 "/home/ponomar/v/ENV/lib/python2.6/site-packages/Mako-0.5.0-py2.6.egg/mako/runtime.py",第 718 行,在 _exec_template 中

callable_(context, *args, **kwargs)

文件 "pyramid_debugtoolbar_panels_templates_headers_mako",第 34 行,在 render_body 中

文件 "/home/ponomar/v/ENV/lib/python2.6/site-packages/MarkupSafe-0.15-py2.6.egg/markupsafe/_native.py",第 21 行,在 escape 中

return Markup(unicode(s)

出现了 MakoRenderingException:

追踪信息(最近的调用在最前面):

文件 "/home/ponomar/v/ENV/lib/python2.6/site-packages/pyramid-1.3a3-py2.6.egg/pyramid/mako_templating.py",第 154 行,在 call

result = template.render_unicode(**system)

文件 "/home/ponomar/v/ENV/lib/python2.6/site-packages/Mako-0.5.0-py2.6.egg/mako/template.py",第 311 行,在 render_unicode 中

as_unicode=True)

文件 "/home/ponomar/v/ENV/lib/python2.6/site-packages/Mako-0.5.0-py2.6.egg/mako/runtime.py",第 660 行,在 _render 中

**_kwargs_for_callable(callable_, data))

文件 "/home/ponomar/v/ENV/lib/python2.6/site-packages/Mako-0.5.0-py2.6.egg/mako/runtime.py",第 692 行,在 _render_context 中

_exec_template(inherit, lclcontext, args=args, kwargs=kwargs)

文件 "/home/ponomar/v/ENV/lib/python2.6/site-packages/Mako-0.5.0-py2.6.egg/mako/runtime.py",第 718 行,在 _exec_template 中

callable_(context, *args, **kwargs)

文件 "/home/ponomar/v/ENV/lib/python2.6/site-packages/pyramid_debugtoolbar-0.9.7-py2.6.egg/pyramid_debugtoolbar/templates/toolbar.mako",第 61 行,在 render_body 中

${panel.content()|n}

文件 "/home/ponomar/v/ENV/lib/python2.6/site-packages/pyramid_debugtoolbar-0.9.7-py2.6.egg/pyramid_debugtoolbar/panels/headers.py",第 54 行,在 content 中

vars, self.request)

文件 "/home/ponomar/v/ENV/lib/python2.6/site-packages/pyramid_debugtoolbar-0.9.7-py2.6.egg/pyramid_debugtoolbar/panels/init.py",第 24 行,在 render 中

return render(template_name, vars, request=request)

文件 "/home/ponomar/v/ENV/lib/python2.6/site-packages/pyramid-1.3a3-py2.6.egg/pyramid/renderers.py",第 81 行,在 render 中

return helper.render(value, None, request=request)

文件 "/home/ponomar/v/ENV/lib/python2.6/site-packages/pyramid-1.3a3-py2.6.egg/pyramid/renderers.py",第 420 行,在 render 中

result = renderer(value, system_values)

文件 "/home/ponomar/v/ENV/lib/python2.6/site-packages/pyramid-1.3a3-py2.6.egg/pyramid/mako_templating.py",第 162 行,在 call

reraise(MakoRenderingException(errtext), None, exc_info[2])

文件 "/home/ponomar/v/ENV/lib/python2.6/site-packages/pyramid-1.3a3-py2.6.egg/pyramid/mako_templating.py",第 154 行,在 call

result = template.render_unicode(**system)

文件 "/home/ponomar/v/ENV/lib/python2.6/site-packages/Mako-0.5.0-py2.6.egg/mako/template.py",第 311 行,在 render_unicode 中

as_unicode=True)

文件 "/home/ponomar/v/ENV/lib/python2.6/site-packages/Mako-0.5.0-py2.6.egg/mako/runtime.py",第 660 行,在 _render 中

**_kwargs_for_callable(callable_, data))

文件 "/home/ponomar/v/ENV/lib/python2.6/site-packages/Mako-0.5.0-py2.6.egg/mako/runtime.py",第 692 行,在 _render_context 中

_exec_template(inherit, lclcontext, args=args, kwargs=kwargs)

文件 "/home/ponomar/v/ENV/lib/python2.6/site-packages/Mako-0.5.0-py2.6.egg/mako/runtime.py",第 718 行,在 _exec_template 中

callable_(context, *args, **kwargs)

文件 "/home/ponomar/v/ENV/lib/python2.6/site-packages/pyramid_debugtoolbar-0.9.7-py2.6.egg/pyramid_debugtoolbar/panels/templates/headers.mako",第 13 行,在 render_body 中

${value|h}

文件 "/home/ponomar/v/ENV/lib/python2.6/site-packages/MarkupSafe-0.15-py2.6.egg/markupsafe/_native.py",第 21 行,在 escape 中

return Markup(unicode(s)

UnicodeDecodeError: 'ascii' 编码无法解码字节 0xd0 在位置 7: 序号不在范围(128)

1 个回答

2

编码是把Unicode字符串转换成字节字符串。如果你需要反过来做,也就是解码,可以使用解码。比如说,使用 request.POST['title'].decode('utf-8') 这段代码(假设 request.POST['title'] 是一个UTF8编码的字符串)。

撰写回答