Python sqlite3查询结果中的额外字符

2024-04-24 15:26:02 发布

您现在位置:Python中文网/ 问答频道 /正文

因此,我尝试用python运行一个简单的查询,并使用sqlite进行尝试。我得到了正确的查询结果,但我得到了一个额外的“u”在每个文本字段的开头,我不知道为什么。代码如下:

import sqlite3

db = sqlite3.connect(':memory:')
c = db.cursor()

c.execute("create table Students (ID INTEGER Primary key AUTOINCREMENT, FN text, LN text);")

c.execute("INSERT into Students (FN,LN) Values ('FirstName','LastName');")

c.execute("Select * from Students")

for i in c:
    print(i)

我得到的结果是:

(1, u'FirstName', u'LastName')

Process finished with exit code 0

你知道为什么会这样吗? 谢谢


Tags: 代码text文本importexecutedbsqliteconnect
1条回答
网友
1楼 · 发布于 2024-04-24 15:26:02

在Python源代码中,Unicode文本被编写为以uU字符为前缀的字符串。你知道吗

如果您不希望您的单词变成unicode,可以使用unicode-escape编码:

>>> s.encode('unicode-escape')
'FirstName'

相关问题 更多 >