Python 2.7、Appengine 数据存储 &统一码 [英] Python 2.7, Appengine Data Store & Unicode

查看:35
本文介绍了Python 2.7、Appengine 数据存储 &统一码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我今晚读了很多关于 Unicode 的书,因为我想切换到 Jinja2,它要求在应用程序中的任何地方都使用 Unicode.我想我对如何处理它有一个很好的想法,但我想在开始编写我的应用程序之前听听这是否合理:

<块引用>

  1. 处理外部文本输入(通过 html 表单)

    <块引用>

    a) 确保所有 html 页面都是 utf-8 编码的.
    b) 一旦用户按下提交,确保数据在 python 后端收到后立即转换为 Unicode...decode(self.request.get('stuff'),utf-8)
    c) 保持 unicode,将输出传输到 Jinja2,它将始终使用 utf-8 的默认编码.

  2. 来自 appengine 数据存储区的信息

    <块引用>

    因为 google 将所有内容都存储为 Unicode,所以从数据存储区传入的所有数据都已经是 unicode,我不必担心任何事情(耶!)

  3. 应用内的字符串

    <块引用>

    确保所有 "" 以 u 开头(即 u"hello world"),这将强制所有内容都使用 unicode.

以上是我保持一切一致的策略.还有什么我需要考虑的吗?

谢谢!

解决方案

如果你使用 webapp 或 webapp2,你应该不需要 .decode(self.request.get('stuff'),utf-8.框架尊重指定的数据输入类型.

其他一切看起来都不错.

我也相信

from __future__ import unicode_strings

应该

from __future__ import unicode_literals

并且仅在 2.6 和 2.7 中可用,因此在 App Engine 中它仅在您使用 2.7 时可用

So I've been reading quite a bit about Unicoding tonight because I was thinking of switching to Jinja2, which requires Unicode to be used everywhere within the app. I think I have a good idea of how to deal with it, but I wanted to hear if this is reasonable before I started to code my app:

  1. Dealing with External Text-Inputs (via html forms)

    a) Make sure all html pages are utf-8 encoded.
    b) Once users press submit, make sure the data is converted into Unicode as soon as the python backend receives it...decode(self.request.get('stuff'),utf-8)
    c) Stay in unicode, transfer the outputs to Jinja2 which will always it using the default encoding of utf-8.

  2. Information from the appengine datastore

    Because google stores everything as Unicode, all data coming in from the datastore is already unicode and I don't have to worry about anything (yay!)

  3. Strings within the app

    Make sure all "" start with a u (i.e. u"hello world"), this will force everything to be in unicode.

Well the above is my strategy to keep everything consistent. Is there anything else I need to account for?

thanks!

解决方案

You should not need to .decode(self.request.get('stuff'),utf-8 if you using webapp or webapp2. The framework respects the input type of the data as specified.

Everything else looks right.

Also I believe that

from __future__ import unicode_strings

should be

from __future__ import unicode_literals

and is only available in 2.6 and 2.7 So in App Engine it would only be available if you are using 2.7

这篇关于Python 2.7、Appengine 数据存储 &amp;amp;统一码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆