1

I would appreciate your help with a hopefully trivial issue. Recently I've started learning Python for work with Google App Engine environment. Needlessly to say I began with the simplest Hello World app.

The English version of it works just fine

However, when I try to work with the signs in my mother tongue, then the problem starts. Basically when run on local machine it does not display letters properly.

Here's the piece of code that's causing me issues

# -*- coding: utf-8 -*-
import datetime

print 'Content-Type: text/html'
print ''
print '<html>'
print '<head>'
print '<title>Witaj świecie</title>'
print '</head>'
print '<body>'
print '<h1>Witaj świecie</h1>'
print ''
print 'Data logowania to: %s' % (datetime.datetime.now())
print '</body>'
print '</html>'

Of course I save all files in utf-8 format. Can anyone tell me how to enable proper display of utf-8 characters here?

0

2 Answers 2

3
print '<html>'
print '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">'
print '<head>'
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your help! Now I feel super-stupid. Why I haven't thought of something this obvious...
2

Set the encoding when you set the content type:

print 'Content-Type: text/html; charset=UTF-8'

Don't use a meta tag as @bpgergo suggests; it's specific to HTML, and simply overrides the headers. Since you're already setting the headers, it's easier and better to simply set them correctly in the first place.

You really shouldn't be using CGI and outputting your content using print statements, though - it's messy and will be a real pain to maintain as your app gets bigger, as well as making things like internationalization a lot harder. Instead, use WSGI and templating - see the getting started guide for App Engine, specifically this and this.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.