简体   繁体   English

Google App Engine utf-8 编码问题 Python

[英]Google App Engine utf-8 coding problem with Python

I would appreciate your help with a hopefully trivial issue.感谢您对一个希望微不足道的问题的帮助。 Recently I've started learning Python for work with Google App Engine environment.最近我开始学习 Python 以便在 Google App Engine 环境下工作。 Needlessly to say I began with the simplest Hello World app.不用说,我从最简单的 Hello World 应用程序开始。

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.当然,我以 utf-8 格式保存所有文件。 Can anyone tell me how to enable proper display of utf-8 characters here?谁能告诉我如何在这里正确显示 utf-8 字符?

print '<html>'
print '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">'
print '<head>'

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;不要像@bpgergo 建议的那样使用元标记; it's specific to HTML, and simply overrides the headers.它特定于 HTML,并简单地覆盖标题。 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.但是,您真的不应该使用 CGI 并使用 print 语句输出您的内容 - 它很混乱,并且随着您的应用程序变得越来越大,维护起来会非常痛苦,并且会使诸如国际化之类的事情变得更加困难。 Instead, use WSGI and templating - see the getting started guide for App Engine, specifically this andthis .相反,使用 WSGI 和模板 - 请参阅 App Engine 的入门指南,特别是thisthis

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM