我正在使用Flask和Google App Engine构建Web应用程序.此网络应用程序中的一个页面通过YouTube API拨打电话,以获取带有搜索字词的视频.
我尝试查询时收到以下错误YoutubeVids.html
.
这只发生在我通过Jinja2模板将某个参数传递给页面时.
file "/Users/xxxxx/App-Engine/src/templates/YoutubeVids.html", line 1, in top-level template code {% extends "master.html" %} UnicodeDecodeError: 'ascii' codec can't decode byte 0xe5 in position 0: ordinal not in range(128) INFO 2014-01-27 22:39:40,963 module.py:612] default: "GET /xxx/yyyy HTTP/1.1" 500 291
Vinay Joseph.. 91
弄清楚了.
我把以下内容放在我的python文件的开头
import sys reload(sys) sys.setdefaultencoding("utf-8")
或者您可以使用:from __future__在开始时导入unicode_literals. (2认同)
@idbrii:是的.似乎没有办法获胜(除了Python 3?)! (2认同)
voscausa.. 11
从文档中:Jinja2在内部使用Unicode,这意味着您必须将Unicode对象传递给渲染函数或仅包含ASCII字符的字节串.
Python 2.x中的普通字符串是字节字符串.要使其使用unicode:
byte_string = 'a Python string which contains non-ascii data like €äãü' unicode_string = byte_string.decode('utf-8')
更多:http://blog.notdot.net/2010/07/Getting-unicode-right-in-Python