我正在尝试学习 SQLite 3 的基础知识。我创建了一个表并尝试打印结果:
import sqlite3
def main():
db = sqlite3.connect('test.db')
db.execute('drop table if exists test')
db.execute('create table test (t1 text, i1 int)')
db.execute('insert into test (t1, i1) values (?, ?)', ('one', 1))
db.execute('insert into test (t1, i1) values (?, ?)', ('two', 2))
db.execute('insert into test (t1, i1) values (?, ?)', ('three', 3))
db.execute('insert into test (t1, i1) values (?, ?)', ('four', 4))
db.commit()
cursor = db.execute('select i1, t1 from test order by i1')
for row in cursor:
print (row)
if __name__ == "__main__": main()
打印语句工作正常,但它显示的值如下:
>>>
(1, u'one')
(2, u'two')
(3, u'three')
(4, u'four')
>>>
它包括一个附加字符u
(指定unicode字符串)。如何打印没有此 u
前缀的值?
我注意到这只发生在 Python 2.7 中,而在 Python 3.3.2 中它工作得很好。
最佳答案
您可以unpack 光标
像这样:
for a,b in cursor:
print a,b
请参阅下面的演示:
>>> cursor = [(1, u'one'), (2, u'two'), (3, u'three'), (4, u'four')]
>>> for a,b in cursor:
... print a,b
...
1 one
2 two
3 three
4 four
>>>
关于python - 为什么 SQLite 3 中的值带有 "u"前缀?如何隐藏前缀,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20896053/