简体   繁体   English

我的UTF8字符串中的Django \\ u字符

[英]Django \u characters in my UTF8 strings

I am adding UTF-8 data to a database in Django. 我将utf-8数据添加到Django中的数据库。

As the data goes into the database, everything looks fine - the characters (for example): “Hello” are UTF-8 encoded. 当数据进入数据库时​​,一切看起来都很好-例如,字符:“ Hello”是UTF-8编码的。

My MySQL database is UTF-8 encoded. 我的MySQL数据库是UTF-8编码的。 When I examine the data from the DB by doing a select, my example string looks like this: ?Hello?. 当我通过选择检查来自数据库的数据时,我的示例字符串如下所示:“ Hello”。 I assume this is showing the characters as UTF-8 encoded. 我认为这是将字符显示为UTF-8编码的。

When I select the data from the database in the terminal or for export as a web-service, however - my string looks like this: \“Hello World\”. 但是,当我从终端中的数据库中选择数据或作为Web服务导出时,我的字符串如下所示:\\ u201cHello World \\ u201d。

Does anyone know how I can display my characters correctly? 有谁知道我如何正确显示我的角色?

Do I need to perform some additional UTF-8 encoding somewhere? 我是否需要在某处执行一些其他UTF-8编码?

Thanks, Nick. 谢谢,尼克。

u'\u201cHello World\u201d'

Is the correct Python representation of the Unicode text “Hello World” . 是Unicode文本“Hello World”的正确Python表示形式。 The smartquote characters are being displayed using a \\uXXXX hex escape rather than verbatim because there are often problems with writing Unicode characters to the terminal, particularly on Windows. Smartquote字符是使用\\uXXXX十六进制转义符而不是逐字显示的,因为在终端上写入Unicode字符通常会出现问题,尤其是在Windows上。 (It looks like MySQL tried to write them to the terminal but failed, resulting in the ? placeholders.) (看起来MySQL试图将它们写入终端,但失败了,导致了?占位符。)

On a terminal that does manage to correctly input and output Unicode characters, you can confirm that they're the same thing: 在能够正确输入和输出Unicode字符的终端上,您可以确认它们是相同的:

Python 2.6.5 (r265:79063, Apr 16 2010, 13:57:41) [GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> u'\u201cHello World\u201d'==u'“Hello World”'
True

just as for byte strings, \\x sequences are just the same as characters: 就像字节字符串一样, \\x序列与字符相同:

>>> '\x61'=='a'
True

Now if you've got \\u\u003c/code> or \\x sequences escaping Python and making their way into an exported file, then you've done something wrong with the export. 现在,如果您有\\u\u003c/code>或\\x序列转义了Python并将其导入导出的文件,那么您在导出时就做错了。 Perhaps you used repr() somewhere by mistake. 也许您在错误的地方使用了repr()

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

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