UnicodeDecodeError:'ascii'编解码器无法解码位置0的字节0xe5:序数不在范围内(128)

 平從老 发布于 2023-01-31 00:09

我正在使用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

撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有