如何删除“u”和“前面的数据库表由python显示 [英] how to delete the "u and ' ' " before the of database table display by python

查看:79
本文介绍了如何删除“u”和“前面的数据库表由python显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  --------------- -------------------------------- 
|日期|时间|价格|
---------------------------------------------- -
(u'31 / 05/2013',u'11:10',u'$ 487')
(u'31 / 05/2013',u'11:11' $ 487')
(u'31 / 05/2013',u'11:13',u'$ 487')
(u'31 / 05/2013',u'11:19' u'$ 487')



我希望输出只显示为

  ----------------------------------- ------------ 
|日期|时间|价格|
---------------------------------------------- -
31/05/2013 11:10 $ 487

我不想看 u ''



我的代码的一部分

  cursor.execute(CREATE TABLE如果不存在table2(日期文本,时间文本, )

date = strftime(%d /%m /%Y)
time = strftime(%H:%M)
data1 = [ ,time,eachprice),
]
cursor.executemany('INSERT INTO table2 VALUES(?,?,?)',data1)
conn.commit()
#output
print显示'ipad mini'的历史记录,来自harveynorman
print--------------------------- --------------------
print|日期|时间|价格|
print-------------------------------------------- ---
for cursor.execute('select * from table2')。fetchall():
print row

所以,任何人都可以帮我找出如何删除 g '

解决方案

您正在查看整个元组与unicode字符串;当显示一个包含unicode值的元组时, u''是正常的:

 >>>>打印u'Hello世界!'
Hello World!
>>>> print(u'Hello World',)
(u'Hello World',)



<您要格式化每一行:

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

请参阅 str.format()文档, a href =http://docs.python.org/2/library/string.html#formatstrings =nofollow>格式语法参考;上述格式3值与字段宽度,左对齐每个值到它们的分配的宽度。



宽度是近似的(我没有计算你的



演示:

 >>> row =(u'31 / 05/2013',u'11:10',u'$ 487')
>>> print u'{:< 15} {:< 8} {:< 6}'。格式(* 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


I am trying to use python to create a database and then insert data and display it. However, the output adds a u before every string. What should I do, how can I delete the "u"? the following is the output display:

-----------------------------------------------
| 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')

I want the output only shows like

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

I do not want to see the u and the ''.

the following is a part of my code

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

so, could anyone can help me figure out how to delete the g and ''

解决方案

You are looking at whole tuples with unicode strings; the u'' is normal when showing you a tuple with unicode values inside:

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

You want to format each row:

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

See the str.format() documentation, specifically the Format Syntax reference; the above formats 3 values with field widths, left-aligning each value into their assigned width.

The widths are approximate (I didn't count the number of spaces in your post exactly), but should be easy to adjust to fit your needs.

Demo:

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

or, using a loop and a sequence of row entries:

>>> 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  

这篇关于如何删除“u”和“前面的数据库表由python显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆