我现在处境很困难。在
所以我使用SQLAlchemy和Pyramid作为一个web应用程序。这个应用程序的功能之一是解析表单的输入,表单通过XML-RPC桥传递给Ruby解析器。在
当我试图使用渲染器返回新解析对象的JSON时,就会出现问题。在
以下是错误,后面是详细信息:
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 93: ordinal not in range(128)
设置
数据库设置
排序规则:utf8_general_ci
型号
^{pr2}$查看
@view_config(route_name='citation_add', request_method='POST', renderer='pubs_json')
def citation_add(request):
raw = request.body
citation = parser.parse(raw)[0]
return citation.json
渲染器
# -*- coding: utf-8 -*-
import customjson
import os
from pyramid.asset import abspath_from_asset_spec
class PubsJSONRenderer:
def __init__(self, info):
""" Constructor: info will be an object having the the
following attributes: name (the renderer name), package
(the package that was 'current' at the time the
renderer was registered), type (the renderer type
name), registry (the current application registry) and
settings (the deployment settings dictionary). """
def __call__(self, value, system):
""" Call a the renderer implementation with the value
and the system value passed in as arguments and return
the result (a string or unicode object). The value is
the return value of a view. The system value is a
dictionary containing available system values
(e.g. view, context, and request). """
request = system.get('request')
if request is not None:
if not hasattr(request, 'response_content_type'):
request.response_content_type = 'application/json'
return customjson.dumps(value)
自定义JSON.py
from json import JSONEncoder
from decimal import Decimal
class ExtJsonEncoder(JSONEncoder):
'''
Extends ``simplejson.JSONEncoder`` by allowing it to encode any
arbitrary generator, iterator, closure or functor.
'''
def default(self, c):
# Handles generators and iterators
if hasattr(c, '__iter__'):
return [i for i in c]
# Handles closures and functors
if hasattr(c, '__call__'):
return c()
# Handles precise decimals with loss of precision to float.
# Hack, but it works
if isinstance(c, Decimal):
return float(c)
return JSONEncoder.default(self, c)
def dumps(*args):
'''
Shortcut for ``ExtJsonEncoder.encode()``
'''
return ExtJsonEncoder(sort_keys=False, ensure_ascii=False,
skipkeys=True).encode(*args)
堆栈跟踪
Traceback (most recent call last):
File "/var/site/siteenv/lib/python2.7/site-packages/pyramid/router.py", line 242, in __call__
response = self.invoke_subrequest(request, use_tweens=True)
File "/var/site/siteenv/lib/python2.7/site-packages/pyramid/router.py", line 217, in invoke_subrequest
response = handle_request(request)
File "/var/site/siteenv/lib/python2.7/site-packages/pyramid_debugtoolbar/toolbar.py", line 160, in toolbar_tween
return handler(request)
File "/var/site/siteenv/lib/python2.7/site-packages/pyramid/tweens.py", line 21, in excview_tween
response = handler(request)
File "/var/site/siteenv/lib/python2.7/site-packages/pyramid_tm/__init__.py", line 82, in tm_tween
reraise(*exc_info)
File "/var/site/siteenv/lib/python2.7/site-packages/pyramid_tm/__init__.py", line 63, in tm_tween
response = handler(request)
File "/var/site/siteenv/lib/python2.7/site-packages/pyramid/router.py", line 163, in handle_request
response = view_callable(context, request)
File "/var/site/siteenv/lib/python2.7/site-packages/pyramid/config/views.py", line 329, in attr_view
return view(context, request)
File "/var/site/siteenv/lib/python2.7/site-packages/pyramid/config/views.py", line 305, in predicate_wrapper
return view(context, request)
File "/var/site/siteenv/lib/python2.7/site-packages/pyramid/config/views.py", line 377, in rendered_view
context)
File "/var/site/sitvenv/lib/python2.7/site-packages/pyramid/renderers.py", line 418, in render_view
return self.render_to_response(response, system, request=request)
File "/var/site/siteenv/lib/python2.7/site-packages/pyramid/renderers.py", line 441, in render_to_response
result = self.render(value, system_values, request=request)
File "/var/site/siteenv/lib/python2.7/site-packages/pyramid/renderers.py", line 437, in render
result = renderer(value, system_values)
File "/var/site/renderers.py", line 30, in __call__
return customjson.dumps(value)
File "/var/site/customjson.py", line 38, in dumps
skipkeys=True).encode(*args)
File "/usr/lib/python2.7/json/encoder.py", line 203, in encode
return ''.join(chunks)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 93: ordinal not in range(128)
从解析器返回
我们吃东西
Allen, C. 1995 "It isn't what you think: a new idea about intentional causation." Noûs 29,1:115-126
我们从解析器中返回一个dict对象:
{'title': '\\\\"It isn\\'t what you think: a new idea about intentional causation.\\\\"', 'journal': 'No\\\\xc3\\\\xbbs', 'author': 'Allen, C.', 'number': 1, 'volume': 29, 'date': '1995', 'type': 'article', 'pages': u'115\\u2013126'}
试过了
因为这个应用程序是在虚拟环境中运行的,我觉得跳到page.py
并将默认编码从ascii
改为{
我尝试了编码和解码,并将charset=utf8&use_unicode=1
添加到我的SQLAlchemy URL中,但没有成功。在
我怀疑问题出在customjson.py
文件中的ensure_ascii=False
选项。事实上,Python 2.7 JSON编码器的文档说明了以下内容:
if ensure_ascii is False, some chunks written to fp may be unicode instances. This usually happens because the input contains unicode strings or the encoding parameter is used. Unless fp.write() explicitly understands unicode (as in codecs.getwriter()) this is likely to cause an error.
设置ensure_ascii=True
似乎可以解决这个错误。假设json编码器的默认编码已经是utf-8
,我不确定手动设置它是否有帮助。我需要这些unicode字符,所以我不太确定如何解决这个问题。在
在客户端上有一个对
JSON.stringify
的调用正在转义那些麻烦的字符。删除此项将导致python按预期工作。在相关问题 更多 >
编程相关推荐