Google App Engine:UnicodeDecodeError:'ascii'编解码器无法解码位置48中的字节0xe2:序号不在范围内(128) [英] Google App Engine: UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 48: ordinal not in range(128)
问题描述
我正在使用使用Quora RSS订阅源的Google App Engine开发小型应用程序。有一种形式,并根据用户输入的输入,它将输出与输入相关的链接列表。现在,如果单词用' - '分隔,那么这些应用程序对单字母查询和大部分双字母单词都可以正常工作。但是,对于三个字母的单词和一些双字母单词,我得到以下错误:
UnicodeDecodeError:'ascii'编解码器无法解码48位中的字节0xe2 :序号不在范围内(128)
这是我的Python代码:
import os
import webapp2
从google.appengine.ext导入jinja2
import db
导入urllib2
导入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,** kw):
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)
以下是html代码:
< h1> Quora Live Feed< / h1>< br><<峰; br><峰; br>
{%extendsrssbase.html%}
{%block content%}
{%for e in range(1,19)%}
{{(list [e])}}< br>
< a href ={{linklist [e]}}> {{linklist [e]}}< / a>
< br>< br>
{%endfor%}
{%endblock%}
Python很可能试图用Unicode编解码器将unicode字符串解码为正常的str,并且失败。当你使用unicode数据时,你需要解码它:
content = content.decode('utf-8')
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:
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 48: ordinal not in range(128)
Here's my Python code:
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:
<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 %}
Python is likely trying 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:
content = content.decode('utf-8')
这篇关于Google App Engine:UnicodeDecodeError:'ascii'编解码器无法解码位置48中的字节0xe2:序号不在范围内(128)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!