python将“ E”添加到串 [英] python adds "E" to string
问题描述
此字符串:
"CREATE USER %s PASSWORD %s", (user, pw)
总是扩展为:
CREATE USER E'someuser' PASSWORD E'somepassword'
有人可以告诉我为什么吗?
Can anyone tell me why?
编辑:
上面的扩展字符串是数据库在错误消息中给我的字符串。我正在使用psycopg2访问我的postgres数据库。实际代码如下:
The expanded string above is the string my database gives me back in the error message. I'm using psycopg2 to access my postgres database. The real code looks like this:
conn=psycopg2.connect(user=adminuser, password=adminpass, host=host)
cur = conn.cursor()
#user and pw are simple standard python strings the function gets as parameter
cur.execute("CREATE USER %s PASSWORD %s", (user, pw))
conn.commit()
推荐答案
不仅E,而且引号似乎来自user和pw的类型。 %s只是做str()的工作,这可能会退回到repr(),这两种方法都有相应的方法 __ str __
和 __ repr __
。另外,那不是生成结果的代码(我假设有一个%,但现在只看到一个逗号)。请使用实际的代码,类型和值来扩展您的问题。
Not only the E but the quotes appear to come from whatever type user and pw have. %s simply does what str() does, which may fall back to repr(), both of which have corresponding methods __str__
and __repr__
. Also, that isn't the code that generates your result (I'd assumed there was a %, but now see only a comma). Please expand your question with actual code, types and values.
附录:考虑到它看起来像SQL,我冒昧地猜测您正在看到转义字符串常量,可能是由数据库接口模块或库正确生成的。
Addendum: Considering that it looks like SQL, I'd hazard a guess that you're seeing escape string constants, likely properly generated by your database interface module or library.
这篇关于python将“ E”添加到串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!