处理在 python、tornado 和 redis 上运行的网站中的外来字符
我已经阅读了许多相关问题,但有点不确定如何处理这种情况。
基本问题:处理网站中的“外来”(希伯来语、希腊语、阿拉姆语?等)字符的最佳方法是什么?
我知道我需要使用 UTF-8 编码,但我不知道其背后的机制。
我使用tornado作为我的框架并将数据存储在redis中。
我当前的实现是简单地将等效的英语键盘存储在数据存储中,然后使用适当的希伯来语/希腊语字体(例如 Bwhebb.ttf)在页面上呈现。这在很大程度上是有效的,但我遇到了一些正在 CGI 编码的字符,这反过来又导致字体方法中断。
I've read through many of the related questions and am a bit unsure as to how to handle this situation.
The Basic Question: What is the best way to handle "foreign" (Hebrew, Greek, Aramaic?, etc.) characters in a website?
I get that I need to use UTF-8 encoding but the mechanics behind it are lost on me.
I am using tornado as my framework and am storing the data in redis.
My current implementation is to simply store the English keyboard equivalent in the data store and then rendering on a page with the appropriate Hebrew/Greek font (e.g. Bwhebb.ttf). This has worked, for the most part, but I am bumping up against some characters which are being CGI encoded which, in turn, causes the font method to break.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该将文本保存为 unicode。
虽然当你检索它时,Redis 很可能会给你一个字节字符串,这不是你想要的:
你想将它转换为 unicode,如下所示:
You should save your text in unicode.
Though when you retrieve it, Redis will most probably give you a byte string, which is not what you want:
You want to convert it to unicode like so:
阅读评论中给出的文章。
简而言之,将 unicode 存储在 Redis 中,如果您使用的是 Python 2.x,请始终使用 unicode 字符串 (
u""
)。从 Redis 检索后,您可能必须转换为 unicode (unicode()
),具体取决于它为您提供的内容。Read the articles given in the comments.
Short answer though, store unicode in Redis, and if you're using Python 2.x, use unicode strings (
u""
) throughout. You may have to convert to unicode (unicode()
) after retrieval from Redis, depending on what it gives you.