2013-05-31 43 views
-3

我想用python創建數據庫,然後插入數據並顯示它。 但是,輸出在每個字符串前添加u。 我該怎麼做,我該如何刪除「u」? 以下是輸出顯示:如何在數據庫表顯示之前刪除「u和''」python

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

回答

5

您正在尋找與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 
+0

@MartijinPieters感謝了很多,真正有幫助的,我刨閱讀的鏈接首先是因爲我仍然有很多表需要顯示。 –

相關問題