Python Google App Engine:UnicodeDecodeError:“ascii”编解码器无法解码位置 48 中的字节 0xe2:序号不在范围内 (128)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20906948/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 21:31:12  来源:igfitidea点击:

Google App Engine: UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 48: ordinal not in range(128)

pythongoogle-app-engineunicodejinja2python-unicode

提问by Manas Chaturvedi

I'm working on a small application using Google App Engine which makes use of the Quora RSS feed. There is a form, and based on the input entered by the user, it will output a list of links related to the input. Now, the applications works fine for one letter queries and most of two-letter words if the words are separated by a '-'. However, for three-letter words and some two-letter words, I get the following error:

我正在使用 Google App Engine 开发一个使用 Quora RSS 提要的小型应用程序。有一个表单,根据用户输入的输入,它会输出与输入相关的链接列表。现在,如果单词由“-”分隔,应用程序可以很好地处理单字母查询和大多数双字母单词。但是,对于三个字母的单词和一些两个字母的单词,我收到以下错误:

UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 48: ordinal not in range(128)

UnicodeDecodeError:“ascii”编解码器无法解码位置 48 中的字节 0xe2:序号不在范围内(128)

Here's my Python code:

这是我的 Python 代码:

import os
import webapp2
import jinja2
from google.appengine.ext import db
import urllib2
import re

template_dir = os.path.join(os.path.dirname(__file__), 'templates')
jinja_env = jinja2.Environment(loader = jinja2.FileSystemLoader(template_dir), autoescape=True)

class Handler(webapp2.RequestHandler):
    def write(self, *a, **kw):
        self.response.out.write(*a, **kw)
    def render_str(self, template, **params):
        t = jinja_env.get_template(template)
        return t.render(params)
    def render(self, template, **kw):
        self.write(self.render_str(template, **kw))

class MainPage(Handler):
    def get(self):
        self.render("formrss.html")
    def post(self):
        x = self.request.get("rssquery")
        url = "http://www.quora.com/" + x + "/rss"
        content = urllib2.urlopen(url).read()
        allTitles =  re.compile('<title>(.*?)</title>')
        allLinks = re.compile('<link>(.*?)</link>')
        list = re.findall(allTitles,content)
        linklist = re.findall(allLinks,content)
        self.render("frontrss.html", list = list, linklist = linklist)



app = webapp2.WSGIApplication([('/', MainPage)], debug=True)

Here's the html code:

这是html代码:

<h1>Quora Live Feed</h1><br><br><br>

{% extends "rssbase.html" %}

{% block content %}
    {% for e in range(1, 19) %}
        {{ (list[e]) }} <br>
        <a href="{{ linklist[e] }}">{{ linklist[e] }}</a>
        <br><br>
    {% endfor %}
{% endblock %}

采纳答案by Jon Wayne Parrott

Python is likelytrying to decode a unicode string into a normal str with the ascii codec and is failing. When you're working with unicode data you need to decode it:

Python可能会尝试使用 ascii 编解码器将 unicode 字符串解码为普通 str 并且失败。当您使用 unicode 数据时,您需要对其进行解码:

content = content.decode('utf-8')

回答by KimKha

In my AppEngine app, I convert it like:

在我的 AppEngine 应用程序中,我将其转换为:

content = unicode(content)

I think it more clear and easy to use.

我认为它更清晰,更易于使用。