用Python中的sql将一行合并到一个coulmn中

用Python中的sql将一行合并到一个coulmn中,python,sql,list,row,Python,Sql,List,Row,我用python创建了一个sql文件,从imdb.txt文件中插入了一些数据 我的代码是: 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.

我用python创建了一个sql文件,从imdb.txt文件中插入了一些数据

我的代码是:

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文件非常庞大,因此这相当于输出的0.1%

 ( 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或一些基本python来实现这一点吗?

当您在行中迭代时,标题将是一个可以从元组中引用的项目-如果您只想打印它,请使用

print row[1]
如果要收集python列表中的所有标题

titleList = []
....
for row in c.execute('SELECT * FROM imdb1 order by ROWID'):
    titleList.append(row[1])
如果只需要标题,还可以将SELECT语句从“SELECT*…”更改为“SELECT Title”,这将使标题值成为行中的唯一项(在第[0]行)


注意,我不确定打印出来的列标题,也不确定示例中的最后一行,它看起来不仅仅是一个标题。

我已经更改了上面帖子中的一些内容