【问题标题】:how to delete the "u and ' ' " before the of database table display by python [closed]如何在python显示的数据库表之前删除“u和''” [关闭]
【发布时间】:2013-05-27 16:06:02
【问题描述】:

我正在尝试使用 python 创建一个数据库,然后插入数据并显示它。 但是,输出会在每个字符串之前添加一个u。 我该怎么办,如何删除“u”? 以下是输出显示:

-----------------------------------------------
| Date         | Time    | Price      |
-----------------------------------------------
(u'31/05/2013', u'11:10', u'$487')
(u'31/05/2013', u'11:11', u'$487')
(u'31/05/2013', u'11:13', u'$487')
(u'31/05/2013', u'11:19', u'$487')

我希望输出只显示像

-----------------------------------------------
| Date         | Time    | Price      |
-----------------------------------------------
 31/05/2013       11:10     $487

我不想看到u''

以下是我的代码的一部分

cursor.execute("CREATE TABLE if not exists table2 (date text, time text, price real)")

date=strftime("%d/%m/%Y")
time=strftime("%H:%M")
data1 = [(date,time,eachprice),
        ]
cursor.executemany('INSERT INTO table2 VALUES (?,?,?)', data1)
conn.commit()
#output
print "Showing history for 'ipad mini', from harveynorman"
print "-----------------------------------------------"
print "| Date         | Time    | Price      |"
print "-----------------------------------------------"
for row in cursor.execute('select * from table2').fetchall():
       print row

那么,谁能帮我弄清楚如何删除g''

【问题讨论】:

    标签: python database


    【解决方案1】:

    您正在查看带有 unicode 字符串的整个元组; u'' 在向您显示内部包含 unicode 值的元组时是正常的:

    >>> print u'Hello World!'
    Hello World!
    >>> print (u'Hello World',)
    (u'Hello World',)
    

    你想格式化每一行:

    print u' {:<15} {:<8} {:<6}'.format(*row)
    

    str.format() documentation,特别是Format Syntax reference;上述格式 3 个具有字段宽度的值,将每个值左对齐为其分配的宽度。

    宽度是近似值(我没有准确计算您帖子中的空格数),但应该很容易调整以满足您的需求。

    演示:

    >>> row = (u'31/05/2013', u'11:10', u'$487')
    >>> print u' {:<15} {:<8} {:<6}'.format(*row)
     31/05/2013      11:10    $487  
    

    或者,使用循环和一系列行条目:

    >>> rows = [
    ... (u'31/05/2013', u'11:10', u'$487'),
    ... (u'31/05/2013', u'11:11', u'$487'),
    ... (u'31/05/2013', u'11:13', u'$487'),
    ... (u'31/05/2013', u'11:19', u'$487'),
    ... ]
    >>> for row in rows:
    ...     print u' {:<15} {:<8} {:<6}'.format(*row)
    ... 
     31/05/2013      11:10    $487  
     31/05/2013      11:11    $487  
     31/05/2013      11:13    $487  
     31/05/2013      11:19    $487  
    

    【讨论】:

    • @MartijinPieters 非常感谢,真的很有帮助,我打算先阅读链接,因为我还有很多表格需要显示。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-07-21
    • 1970-01-01
    • 1970-01-01
    • 2015-07-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多