从Web服务获取的数据中获取UnicodeEncodeError错误

时间:2017-08-06 11:13:55

标签: string ascii jinja2 python-unicode unicode-string

我正在接受网络服务的跟随json,

>>> data["city"]
{u'country': u'SA', u'population': 0, u'id': 109771, u'coord': {u'lat': 24.4765, u'lon': 46.2499}, u'name': u'Al Gha\u0163gha\u0163'  }

问题是data["city"]["name"]我的烧瓶应用程序中出现错误:

UnicodeEncodeError: 'ascii' codec can't encode character u'\u0163' in position 6: ordinal not in range(128)

我该如何解决?

我做了一些研究,城市名称是南非人物,所以我相信如果我解码像city.decode(" south-african-decode-type")可能会修复,但我不知道是什么编码南非使用?

发生错误的地方:  response = make_response(render_template(" index.html",forcast_list = forcast_list,         lat = lat,lng = lng,city = city,country = country,count = count或request.cookies.get(" count")))

完整Stacktrace:

Traceback (most recent call last):
  File "/Users/ciastoPiekarz/Development/web/python/flask/flsk-dimnd/venv/lib/python2.7/site-packages/flask/app.py", line 2000, in __call__
    return self.wsgi_app(environ, start_response)
  File "/Users/ciastoPiekarz/Development/web/python/flask/flsk-dimnd/venv/lib/python2.7/site-packages/flask/app.py", line 1991, in wsgi_app
    response = self.make_response(self.handle_exception(e))
  File "/Users/ciastoPiekarz/Development/web/python/flask/flsk-dimnd/venv/lib/python2.7/site-packages/flask/app.py", line 1567, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/Users/ciastoPiekarz/Development/web/python/flask/flsk-dimnd/venv/lib/python2.7/site-packages/flask/app.py", line 1988, in wsgi_app
    response = self.full_dispatch_request()
  File "/Users/ciastoPiekarz/Development/web/python/flask/flsk-dimnd/venv/lib/python2.7/site-packages/flask/app.py", line 1641, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/Users/ciastoPiekarz/Development/web/python/flask/flsk-dimnd/venv/lib/python2.7/site-packages/flask/app.py", line 1544, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/Users/ciastoPiekarz/Development/web/python/flask/flsk-dimnd/venv/lib/python2.7/site-packages/flask/app.py", line 1639, in full_dispatch_request
    rv = self.dispatch_request()
  File "/Users/ciastoPiekarz/Development/web/python/flask/flsk-dimnd/venv/lib/python2.7/site-packages/flask_debugtoolbar/__init__.py", line 124, in dispatch_request
    return view_func(**req.view_args)
  File "/Users/ciastoPiekarz/Development/web/python/weatherApp/src/controller.py", line 115, in index
    lat=lat, lng=lng, city=city,country=country, count=count or request.cookies.get("count")))
  File "/Users/ciastoPiekarz/Development/web/python/flask/flsk-dimnd/venv/lib/python2.7/site-packages/flask/templating.py", line 134, in render_template
    context, ctx.app)
  File "/Users/ciastoPiekarz/Development/web/python/flask/flsk-dimnd/venv/lib/python2.7/site-packages/flask/templating.py", line 116, in _render
    rv = template.render(context)
  File "/Users/ciastoPiekarz/Development/web/python/flask/flsk-dimnd/venv/lib/python2.7/site-packages/jinja2/environment.py", line 989, in render
    return self.environment.handle_exception(exc_info, True)
  File "/Users/ciastoPiekarz/Development/web/python/flask/flsk-dimnd/venv/lib/python2.7/site-packages/jinja2/environment.py", line 754, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/Users/ciastoPiekarz/Development/web/python/weatherApp/templates/index.html", line 23, in top-level template code
    <h1>Weather forcast for {{ city }}, {{ country }} </h1>
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc5 in position 6: ordinal not in range(128)

0 个答案:

没有答案