Django 解码 UTF 字符 - \\u0411\\u0435\\u0441\\u0435\\u0434\\u043a\\u0430 - 为西里尔字符串

发布于 2024-11-07 21:11:50 字数 465 浏览 6 评论 0原文

我正在使用 Django 1.3。 请您能好心回答我一个问题吗? 我正在从数据库中读取数据,其中编码设置为 untf8-unicode

settings.py
DEFAULT_CHARSET = 'utf-8'

file.py
# -*- coding: utf-8 -*-
def get_gift(gift_id):
    gift = Gift.objects.get(id__exact = gift_id, is_visible = True)
    return gift

def output():
    gift = get_gift(gift_id)
    title = gift.name.encode('utf-8')
    return HttpResponse(title)

作为响应,我收到 \u0411\u0435\u0441\u0435\u0434\u043a\u0430,但它应该是俄语(西里尔文)

I am using Django 1.3.
Would you be so kind and answer me one question.
I am reading data from my database, where encoding is set to untf8-unicode

settings.py
DEFAULT_CHARSET = 'utf-8'

file.py
# -*- coding: utf-8 -*-
def get_gift(gift_id):
    gift = Gift.objects.get(id__exact = gift_id, is_visible = True)
    return gift

def output():
    gift = get_gift(gift_id)
    title = gift.name.encode('utf-8')
    return HttpResponse(title)

In response I am getting \u0411\u0435\u0441\u0435\u0434\u043a\u0430, but it should be in Russian (Cyrillic)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

黎歌 2024-11-14 21:11:50

经常发生这样的情况:您有 Unicode 格式的非罗马文本数据,但无法显示它 — 通常是因为您尝试通过不支持 Unicode 的应用程序将其显示给用户,或者因为您使用的字体需要无法获得。您可以将 Unicode 字符表示为“??????”或“\15BA\15A0\1610...”,但这对于真正想要阅读文本内容的用户来说几乎毫无用处。

Unidecode 提供的是一个函数“unidecode(...)”,它获取 Unicode 数据并尝试用 ASCII 表示它(即 0x00 到 0x7F 之间的通用可显示字符)。

这种表示几乎总是音译的尝试——即,用罗马字母传达文本在其他书写系统中所表达的发音。 (参见上面的示例)

更多信息

尝试pip install Unidecode

It often happens that you have non-Roman text data in Unicode, but you can't display it -- usually because you're trying to show it to a user via an application that doesn't support Unicode, or because the fonts you need aren't accessible. You could represent the Unicode characters as "???????" or "\15BA\15A0\1610...", but that's nearly useless to the user who actually wants to read what the text says.

What Unidecode provides is a function, 'unidecode(...)' that takes Unicode data and tries to represent it in ASCII (i.e., the universally displayable characters between 0x00 and 0x7F).

The representation is almost always an attempt at transliteration -- i.e., conveying, in Roman letters, the pronunciation expressed by the text in some other writing system. (See the example above)

More information here

try pip install Unidecode

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文