python - 为什么 SQLite 3 中的值带有 "u"前缀?如何隐藏前缀

标签 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 (?, ?)', ('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/

相关文章:

python - 如何停止重复出现错误消息或找出错误消息的来源

python - Django 单元测试客户端登录 : fails in test suite, 但不在 Shell 中

ios - 是否可以通过核心数据以编程方式更改表

sqlite - 如何将sql server 2008 R2与windows store应用程序连接起来

iOS SQLite 数据存储 - 更新 VS。重新创造

python - 如果所有行在其列中只有一个值,则折叠 Pandas 数据框中的行

Python 覆盖列表并调整列表大小

python - PyQt 使用 ctrl+Enter 触发按钮

android - 如何为 SQLite 数据库更改实现单步撤消?

android - 在 Android 中保存动态生成的 UI(是否使用 Sqlite?)