Python unicode 与 utf-8
Python unicode vs utf-8
我正在构建一个字符串查询(密码查询)以针对数据库 (Neo4J) 执行它。
我需要连接一些字符串,但我遇到了编码问题。
我正在尝试构建一个 unicode 字符串。
# -*- coding: utf-8 -*-
value = u"D'Santana Carlos Lãnez"
key = u"Name"
line = key + u" = "+ repr(value)
print line.encode("utf-8")
我预计有:
Name = "D'Santana Carlos Lãnez"
但我得到:
Name = u"D'Santana Carlos L\xe3nez"
我想 repr
正在返回一个 unicode。或者可能我没有使用正确的功能。
value
已经是 unicode 因为你在 u"..."
中使用前缀 u
所以你不需要 repr()
(和 unicode()
或 decode()
)
此外 repr()
不会转换为 unicode。但它 returns 字符串对于调试非常有用 - 它显示本机字符和其他内容的十六进制代码。
value = u"D'Santana Carlos Lãnez"
key = u"Name"
line = key + u" = "+ value
print(line)
Python 文字 (repr
) 语法不是 Cypher 字符串文字语法的有效替代。前导 u
只是它们之间的区别之一;值得注意的是,Cypher 字符串文字没有 \x
转义,Python 将用于 U+0080–U+00FF 之间的字符。
如果您需要从 Python 字符串创建 Cypher 字符串文字,您需要编写自己的字符串转义函数来写入输出匹配 that syntax. But you should generally avoid creating queries from variable input. As with SQL databases, the better answer is query parameterisation.
我正在构建一个字符串查询(密码查询)以针对数据库 (Neo4J) 执行它。
我需要连接一些字符串,但我遇到了编码问题。 我正在尝试构建一个 unicode 字符串。
# -*- coding: utf-8 -*-
value = u"D'Santana Carlos Lãnez"
key = u"Name"
line = key + u" = "+ repr(value)
print line.encode("utf-8")
我预计有:
Name = "D'Santana Carlos Lãnez"
但我得到:
Name = u"D'Santana Carlos L\xe3nez"
我想 repr
正在返回一个 unicode。或者可能我没有使用正确的功能。
value
已经是 unicode 因为你在 u"..."
中使用前缀 u
所以你不需要 repr()
(和 unicode()
或 decode()
)
此外 repr()
不会转换为 unicode。但它 returns 字符串对于调试非常有用 - 它显示本机字符和其他内容的十六进制代码。
value = u"D'Santana Carlos Lãnez"
key = u"Name"
line = key + u" = "+ value
print(line)
Python 文字 (repr
) 语法不是 Cypher 字符串文字语法的有效替代。前导 u
只是它们之间的区别之一;值得注意的是,Cypher 字符串文字没有 \x
转义,Python 将用于 U+0080–U+00FF 之间的字符。
如果您需要从 Python 字符串创建 Cypher 字符串文字,您需要编写自己的字符串转义函数来写入输出匹配 that syntax. But you should generally avoid creating queries from variable input. As with SQL databases, the better answer is query parameterisation.