Google App Engine在StringProperty v StringListProperty objs中显示的unicode不同吗? [英] does google app engine display unicode differently in StringProperty v StringListProperty objs?

查看:39
本文介绍了Google App Engine在StringProperty v StringListProperty objs中显示的unicode不同吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个db.StringProperty()mRegion,它设置为某些韩文文本.我在仪表板上看到这样的值显然是韩文的:

I have a db.StringProperty() mRegion that is set to some Korean text. I see in my Dashboard that the value is visibly in Korean like this:

한국:충청남도

但是,当我使用此字段并将其添加到字符串列表属性(db.StringListProperty())时,我最终得到以下结果:

However, when I take this field and add it into a string list property (db.StringListProperty()) I end up with something like this:

\ ud55c \ uad6d:\ ucda9 \ uccad \ ub0a8 \ ub3c4

\ud55c\uad6d : \ucda9\uccad\ub0a8\ub3c4

当我将此字符串列表属性值输出到客户端时,我在客户端上显示此文本时遇到了问题,因此,我想知道存储该值时服务器端是否出了问题(正如我期望的那样)像StringProperty一样易于阅读的韩语.)

I am having issues displaying this text on my client when I have this string list property value output to the client, so it makes me wonder if something is wrong on the server end when the value is stored (as I would expect it to be readable Korean like the StringProperty).

有人知道我可能会在哪里出错吗,或者第二个显示在字符串列表对象中仅仅是正常的,并且问题可能出在我的客户端上?

Does anyone know where I might be going wrong with this or if this second display is simply normal in string list objects and the problem is likely on my client end?

谢谢.

更新有关问题的更多详细信息:我的客户是一个iPhone应用程序.基本上,我使用iPhone通过反向地址解析器api获取用户的gps位置信息.我将其发送到应用程序引擎并保存.这部分似乎有效,因为对于韩国来说,我看到了朝鲜语字符.总的来说,区域名称是这样获得的:

Update with more detail of the issues: My client is an iphone app. Basically, I use the iPhone to get the user's gps location info using the reverse geocoder api. I send this to app engine and save it. This part appears to be working because for Korea, I see the Korean characters. The region name is obtained, in summary, like this:

region = self.request.get('region')
entry.init(region)
...
self.mRegion = region

非常简单(并且有效).

pretty straightforward (and it works).

当我检索该数据并将其发送回客户端时,它就会崩溃.总结一下:

Where it breaks down is when I retrieve that data and send it back to the client. To summarize:

query = db.GqlQuery("SELECT * FROM RegionData WHERE mLatitudeCenter >= :1 and mLatitudeCenter <= :2", latmin, latmax)
for entry in query:
        output += entry.mRegion + ','
self.response.out.write(output)

当我把它放在客户端的UILabel上时,它显示为乱码.另外,当我在客户端中获取乱码时,将其 back 发送给服务器以查找区域,则该操作失败,因此这向我暗示,与其发送韩文,不如说是发送了repr()字符或其他内容.如您所说,如果只是表示问题而不是固有数据本身,那么这可能与我用来显示此数据的系统字体有关?我曾经以为我在某个地方缺少正确的对encode()或decode()的调用,但不确定.

When I take this and put it on a UILabel in the client, it's garbled. Also, when I take the garbled value in the client and send it back to the server to look up a region, it fails, so that suggests to me that instead of sending the Korean text maybe it's transmitting the repr() characters or something. If, as you say, it's just a matter of presentation and not the inherent data itself, then perhaps it's something to do with the system font I'm using to try to display this data? I had thought that somewhere I was missing the right call to encode() or decode(), but not sure.

推荐答案

管理界面很有可能以不同的方式显示两个,是的.在后一种情况下,它显然在执行repr,而在前一种情况下,它只是在打印字符串.

It's quite possible that the admin interface displays the two differently, yes. In the latter case it's clearly doing a repr(s), while in the former it's just printing the string.

尽管如此,管理界面的界面不会影响代码的工作方式-字符串和StringLists都以相同的方式存储在数据存储区中,并且会以Unicode字符串的形式返回,供您随意处理.

The admin interface's interface doesn't affect how your code works, though - both Strings and StringLists are stored the same way in the datastore, and will come back as Unicode strings for you to deal with as you wish.

我强烈建议您阅读这本有关Joel的关于统一码的文章.简而言之,您要处理两种情况:二进制数据和unicode字符.为了使您感到困惑,Python将它们都公开为字符串-分别为"unicode字符串"和原始字符串",但是您仅应将前者视为实际字符串.

I highly recommend reading this Joel on Software post about unicode. In short, you're dealing with two kinds of things: Binary data, and unicode characters. To confuse you, Python exposes these both as strings - 'unicode strings' and 'raw strings', respectively, but you should only treat the former as actual strings.

数据存储区及其StringListProperty和StringProperty存储并返回Unicode字符串.您的框架还应该为您提供Unicode字符串,并接受Unicode字符串,但是某些设计欠佳的框架却没有.

The datastore, with its StringListProperty and StringProperty, stores and returns Unicode strings. Your framework should also be giving you Unicode strings, and accepting Unicode strings back, but some poorly designed frameworks don't.

您需要做的是检查在处理文本的任何地方都使用Unicode字符串,是否显式调用.encode()将Unicode字符串转换为原始字符串,以及.decode()转换为原始字符串.设置为unicode字符串,并且已正确设置返回的响应上的字符编码,并且您正在使用相同的编码对字符串进行编码.如何执行将取决于您的框架.

What you need to do is check that you are using Unicode strings everywhere you deal with text, that you explicitly call .encode() to convert a Unicode string to a raw string, and .decode() to convert a raw string to a unicode string, and that the character encoding on the returned response is set correctly, and you're encoding your strings using the same encoding. How you do that will depend on your framework.

完成此操作后,如果仍然遇到问题,我建议编写一些简单的单元测试-将数据存储到数据存储中,然后检索并处理它,然后检查它是否等于您的期望-以确定问题是.

Once you've done that, if you still have trouble, I would suggest writing some simple unit tests - storing data to the datastore and retrieving it and manipulating it, then checking it equals what you expect - to pin down where the issue is.

这篇关于Google App Engine在StringProperty v StringListProperty objs中显示的unicode不同吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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