Python 为什么SQLite 3中的值显示为“0”;u";前缀如何隐藏前缀
我正在努力学习SQLite 3的基础知识。我创建了一个表并尝试打印结果:Python 为什么SQLite 3中的值显示为“0”;u";前缀如何隐藏前缀,python,sqlite,Python,Sqlite,我正在努力学习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 (?,
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()
print语句工作正常,但它显示如下值:
>>>
(1, u'one')
(2, u'two')
(3, u'three')
(4, u'four')
>>>
它包括一个附加字符u
(表示unicode字符串)。没有此u
前缀,如何打印值
我注意到这只发生在Python2.7中,而在Python3.3.2中效果很好。我建议您这样做
for row in cursor:
print(row[0], row[1])
尽管如此,我怀疑您是否正在运行Python3.x
print((1, u'aaa'))
屈服
(1, 'aaa')
在Python 3.3上,以及
(1, u'aaa')
在Python2.7上。您可以像这样cursor
:
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
>>>
我也知道这意味着什么,我只是不希望它出现在print语句中。所以这个问题是不同的。@66:它的出现是因为您打印的是一个
元组
对象,而不是字符串。tuple对象使用repr
而不是str
表示其内容。