2014-05-10 64 views
1

我已經創建了一個SQL文件與python從imdb.txt文件插入一些數據。結合在一個coulmn行與SQL在Python

我的代碼,以便票價:

import re 
import sqlite3 
conn = sqlite3.connect('imdb1.db') 
c = conn.cursor() 

c.execute('''CREATE TABLE imdb1 (ROWID INTEGER PRIMARY KEY, Title, Rating)''') 

x = open("ratings.list.txt","r") 
movread = x.readlines() 
x.close() 



s = raw_input('Search: ').lower() 
for ns in movread: 


    if s in ns.lower(): 
     d = re.split('\s+',ns,4) 
     Title = d[4].rstrip() 
     Rating= d[3] 

     list = [Title,Rating] 

     # print list 
     # Insert a row of data 
     c.execute('INSERT INTO imdb1 (Title, Rating) values (?, ?)', (list[0],list[1])) 
     conn.commit() 

for row in c.execute('SELECT * FROM imdb1 order by ROWID'): 
     print row 

這是輸出當我打印該行:

ROWID      Title       Rating 

(1, u'The Lord of the Rings: The Return of the King (2003)', u'8.9') 
(2, u'The Lord of the Rings: The Fellowship of the Ring (2001)', u'8.8') 
(3, u'The Lord of the Rings: The Two Towers (2002)', u'8.7') 
(4, u'"5 Second Movies" (2007) {The Lord of the Rings and the Two Towers (#1.63)}', u'6.2') 

我想獲得的所有冠軍,在一個列表。就像這樣:

注意,IMDB文件是巨大的,所以這是像輸出

(u'The Lord of the Rings: The Return of the King (2003)', 
    u'The Lord of the Rings: The Fellowship of the Ring (2001)', 
    u'The Lord of the Rings: The Two Towers (2002)', 
    u'"5 Second Movies" (2007) {The Lord of the Rings and the Two Towers (#1.63)}') 

我可以以某種方式與SQL或一些基本的蟒蛇做到這一點的0.1%?

+0

我改變了以上 –

回答

1

當你通過你的行重複標題將是您可以從元組中引用的項目 - 如果你只是想打印出來使用

print row[1] 

如果你想收集所有冠軍在Python列表,

titleList = [] 
.... 
for row in c.execute('SELECT * FROM imdb1 order by ROWID'): 
    titleList.append(row[1]) 

你也可以從「SELECT * ...」到「選擇標題」如果你想要的是冠軍,這將使標題值的行中唯一的項目(在改變你的SELECT語句行[0])。

警告,我不確定在那裏打印的列標題,或者關於您想要的內容的示例中的最後一行,不僅僅是標題。

+0

以上的帖子中的som事情:D謝謝 –