Skip to content Skip to sidebar Skip to footer

Problems With Python In Google App Engine - UTF-8 And ASCII

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

Solution 1:

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.


Solution 2:

@Thomas K. Thank you for your guidance here. Thanks to you I was able to come up with, maybe - as you said - a little roudabout solution - so the credit for the answer should go to you. The following line of code:

Messages.append(ChatMessage(chatter, msg))

Should look like this:

Messages.append(ChatMessage(chatter.encode( "utf-8" ), msg.encode( "utf-8" )))

Basically I have to encode all the utf-8 string to ascii.


Post a Comment for "Problems With Python In Google App Engine - UTF-8 And ASCII"