Google App Engine中的Python问题 - UTF-8和ASCII [英] Problems with Python in Google App Engine - UTF-8 and ASCII
问题描述
我从书中的'云中的代码'中找到以下一段简单的聊天室代码片段。
from google.appengine.ext从google.appengine.ext.webapp.util导入webapp
导入run_wsgi_app
导入日期时间
$ b $#START:MainPage $ b $ class ChatMessage(object):
def __init __(self,user,msg):
self.user = user
self .message = msg
self.time = datetime.datetime.now()
def __str __(self):
return%s(%s):%s% (self.user,self.time,self.message)
Messages = []
$ b $ class ChatRoomPage(webapp.RequestHandler):
def get(self) :
self.response.headers [Content-Type] =text / html
self.response.out.write(
< html>
< head>
< title> MarkCC的AppEngine聊天室< / title>
< / head>
< body>
< h1>欢迎来到MarkCC的AppEngine聊天室< / h1>
< p>(当前时间为%s)< / p>
%(datetime.datetime.now()))
#输出一组聊天消息
全局消息
用于消息中的消息:
self。 response.out.write(< p>%s< / p>%msg)
self.response.out.write(
< form action =method = >
< div>< b>名称:< / b>
< textarea name =namerows =1cols =20>< < / gt>< / div>
< p>< b>消息< / b>< / p>
< div>< textarea name =messagerows =5 cols =60>< / textarea>< / div>
< div>< input type =submitvalue =发送ChatMessage>< / input>< / div> ;
< / form>
< / body>
< / html>
)
#END:MainPage
#START :PostHandler
def post(self):
chatter = self.request.get(name)
msg = self.request.get(message)
全局消息
Messages.append(ChatMe ssage(chatter,msg))
#现在我们已经将消息添加到聊天中,我们将
#重定向到根页面,这会使用户的浏览器刷新到
#显示聊天内容,包括他们的新消息。
self.redirect('/')
#END:PostHandler
#START:Frame
chatapp = webapp.WSGIApplication([('/',ChatRoomPage)])
def main():
run_wsgi_app(chatapp)
if __name__ = =__main__:
main()
#END:Frame
以英语行事。然而,当我添加一些非标准字符时,所有类型的问题都开始了。首先,为了使事物能够在HTML中显示字符,我首先需要添加元标记 - 字符集= UTF-8等等
奇怪的是,如果输入非标准字母,程序会很好地处理它们,并且不会显示任何问题。 ,如果我用脚本输入任何非ascii字母到web布局,我就知道添加utf-8编码行会起作用,所以我添加了(# - - coding:utf-8 - - )。这是不够的,当然我忘了用UTF-8格式保存文件,然后程序开始运行。将是故事的好结局,唉....
它不起作用
长故事简短的代码:
# - * - 编码:utf-8 - * -
from google.appengine。 ext从google.appengine.ext.webapp.util导入webapp
导入run_wsgi_app
导入datetime
$ b $#START:MainPage $ b $ class ChatMessage(object):
def __init __(self,user,msg):
self.user = user
self.message = msg
self.time = datetime.datetime.now()
$ b $ def __str __(self):
return%s(%s) :%s%(self.user,self.time,self.message)
Messages = [] $ b $ class ChatRoomPage(webapp.RequestHandler):
def get(self ):
self.response.headers [Content-Type] =text / html
self.response.out.write(
< html>
< head>
< title> Witaj w pokoju czatu MarkCC w App Engine< / title>
< meta http-equiv =Content-Typecontent =text / html; charset = UTF-8>
< / head>
< body>
< h1> Witaj w pokoju czatu MarkCC w App Engine< / h1>
< p>(Dokladny czas Twojego logowania to:%s)< / p>
%(datetime.datetime.now()))
#输出一组聊天消息
全局消息
用于消息中的消息:
self。 response.out.write(< p>%s< / p>%msg)
self.response.out.write(
< form action =method = < / b>
< textarea name =namerows =1cols =20>< b> / textarea>< / div>
< p>< b> TwojaWiadomość< / b>< / p>
< div>< textarea name =messagerows = 5cols =60>< / textarea>< / div>
< div>< input type =submitvalue =Send ChatMessage>< / input>< / div>
< / form>
< / body>
< / html>
)
#END:MainPage
#START:PostHandler
def post(self):
chatter = self.request.get(uname)
msg = self.request.get(umessage)
全球消息
消息es.append(ChatMessage(chatter,msg))
#现在我们已经将消息添加到聊天中,我们将
#重定向到根页面,这会使用户的浏览器刷新到
#显示聊天内容,包括他们的新消息。
self.redirect('/')
#END:PostHandler
#START:Frame
chatapp = webapp.WSGIApplication([('/',ChatRoomPage)])
def main():
run_wsgi_app(chatapp)
if __name__ = =__main__:
main()
#END:Frame
失败处理聊天应用程序运行时写入的任何内容。它会加载,但我输入消息的时刻(即使只使用标准字符)我收到
文件D:\Python25\\ \\ lib \StringIO.py,第270行,在getvalue
self.buf + =''.join(self.buflist)
UnicodeDecodeError:'ascii'编解码器无法解码位置为0xc3的字节64:序号不在范围内(128)
错误信息。换句话说,如果我想在应用程序中使用任何字符,我不能在我的界面中放置非英语字符。或者相反,只有在我不用UTF-8编码文件时,我才能在应用程序中使用非英文字符。如何使它们一起工作?
您的字符串包含unicode字符,但它们不是unicode字符串,它们是字节字符串。你需要在每一个前面添加前缀 u
(如 ufoo
),以使它们成为unicode字符串。如果确保所有字符串都是Unicode字符串,则应该消除该错误。
您还应该在 Content-Type < code $>标题而不是元标记,如下所示:
self.response.headers ['Content-Type' ] ='text / html; charset = UTF-8'
请注意,如果您使用模板系统,您的生活会轻松许多内嵌您的Python代码来编写HTML。
So for the past few days I've been trying to learn Python in App Engine. However, I've been encountering a number of problems with ASCII and UTF encoding. The freshest issue is as follows:
I have the following piece of code of a simplistic chatroom from the book 'Code in the Cloud'
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
import datetime
# START: MainPage
class ChatMessage(object):
def __init__(self, user, msg):
self.user = user
self.message = msg
self.time = datetime.datetime.now()
def __str__(self):
return "%s (%s): %s" % (self.user, self.time, self.message)
Messages = []
class ChatRoomPage(webapp.RequestHandler):
def get(self):
self.response.headers["Content-Type"] = "text/html"
self.response.out.write("""
<html>
<head>
<title>MarkCC's AppEngine Chat Room</title>
</head>
<body>
<h1>Welcome to MarkCC's AppEngine Chat Room</h1>
<p>(Current time is %s)</p>
""" % (datetime.datetime.now()))
# Output the set of chat messages
global Messages
for msg in Messages:
self.response.out.write("<p>%s</p>" % msg)
self.response.out.write("""
<form action="" method="post">
<div><b>Name:</b>
<textarea name="name" rows="1" cols="20"></textarea></div>
<p><b>Message</b></p>
<div><textarea name="message" rows="5" cols="60"></textarea></div>
<div><input type="submit" value="Send ChatMessage"></input></div>
</form>
</body>
</html>
""")
# END: MainPage
# START: PostHandler
def post(self):
chatter = self.request.get("name")
msg = self.request.get("message")
global Messages
Messages.append(ChatMessage(chatter, msg))
# Now that we've added the message to the chat, we'll redirect
# to the root page, which will make the user's browser refresh to
# show the chat including their new message.
self.redirect('/')
# END: PostHandler
# START: Frame
chatapp = webapp.WSGIApplication([('/', ChatRoomPage)])
def main():
run_wsgi_app(chatapp)
if __name__ == "__main__":
main()
# END: Frame
It works ok in English. However, the moment I add some non-standard characters all sorts of problems start
First of all, in order for the thing to be actually able to display characters in HTML I add meta tag - charset=UTF-8" etc
Curiously, if you enter non-standard letters, the program processes them nicely, and displays them with no issues. However, it fails to load if I enter any non-ascii letters to the web layout iteself withing the script. I figured out that adding utf-8 encoding line would work. So I added (# -- coding: utf-8 --). This was not enough. Of course I forgot to save the file in UTF-8 format. Upon that the program started running.
That would be the good end to the story, alas....
It doesn't work
Long story short this code:
# -*- coding: utf-8 -*-
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
import datetime
# START: MainPage
class ChatMessage(object):
def __init__(self, user, msg):
self.user = user
self.message = msg
self.time = datetime.datetime.now()
def __str__(self):
return "%s (%s): %s" % (self.user, self.time, self.message)
Messages = []
class ChatRoomPage(webapp.RequestHandler):
def get(self):
self.response.headers["Content-Type"] = "text/html"
self.response.out.write("""
<html>
<head>
<title>Witaj w pokoju czatu MarkCC w App Engine</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<h1>Witaj w pokoju czatu MarkCC w App Engine</h1>
<p>(Dokladny czas Twojego logowania to: %s)</p>
""" % (datetime.datetime.now()))
# Output the set of chat messages
global Messages
for msg in Messages:
self.response.out.write("<p>%s</p>" % msg)
self.response.out.write("""
<form action="" method="post">
<div><b>Twój Nick:</b>
<textarea name="name" rows="1" cols="20"></textarea></div>
<p><b>Twoja Wiadomość</b></p>
<div><textarea name="message" rows="5" cols="60"></textarea></div>
<div><input type="submit" value="Send ChatMessage"></input></div>
</form>
</body>
</html>
""")
# END: MainPage
# START: PostHandler
def post(self):
chatter = self.request.get(u"name")
msg = self.request.get(u"message")
global Messages
Messages.append(ChatMessage(chatter, msg))
# Now that we've added the message to the chat, we'll redirect
# to the root page, which will make the user's browser refresh to
# show the chat including their new message.
self.redirect('/')
# END: PostHandler
# START: Frame
chatapp = webapp.WSGIApplication([('/', ChatRoomPage)])
def main():
run_wsgi_app(chatapp)
if __name__ == "__main__":
main()
# END: Frame
Fails to process anything I write in the chat application when it's running. It loads but the moment I enter my message (even using only standard characters) I receive
File "D:\Python25\lib\StringIO.py", line 270, in getvalue
self.buf += ''.join(self.buflist)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 64: ordinal not in range(128)
error message. In other words, if I want to be able to use any characters within the application I cannot put non-English ones in my interface. Or the other way round, I can use non-English characters within the app only if I don't encode the file in utf-8. How to make it all work together?
Your strings contain unicode characters, but they're not unicode strings, they're byte strings. You need to prefix each one with u
(as in u"foo"
) in order to make them into unicode strings. If you ensure all your strings are Unicode strings, you should eliminate that error.
You should also specify the encoding in the Content-Type
header rather than a meta tag, like this:
self.response.headers['Content-Type'] = 'text/html; charset=UTF-8'
Note your life would be a lot easier if you used a templating system instead of writing HTML inline with your Python code.
这篇关于Google App Engine中的Python问题 - UTF-8和ASCII的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!