使用diff文件创建sqlite数据库

时间:2013-12-19 00:12:21

标签: python sql database sqlite diff

我需要使用diff文件中的数据创建一个sqlite数据库。 diff文件有10列(超过一千行)。我只希望第1,第6,第7,第8和第10列进入数据库。如何在不必逐个添加每一行的情况下执行此操作?

我创建了数据库:

import sqlite3
connection = sqlite3.connect('genes.db')
cursor=connection.cursor()

现在我被困在如何从这里开始。

谢谢!

1 个答案:

答案 0 :(得分:0)

这样的事情应该这样做,但这取决于你的差异输出是什么样的

import sqlite3
connection = sqlite3.connect('genes.db')
cursor = connection.cursor()

# create a table for inserting the data
create = ('CREATE TABLE genes (A text, B text, C text, D text, E text)')
cursor.execute(create)


difffile = """\
one two three four five six seven eight nine ten
a b c d e f g h i j
k l m n o p q r s t\
"""

# rows list for storing the data to insert
rows = []

# loop over lines in difffile
for line in difffile.split("\n"):

    # split the current line by whitespace
    fields = line.split()

    # skip over lines with less than 10 columns
    if len(fields) < 10:
        continue

    # columns we want
    cols = (1, 6, 7, 8, 10)

    # create a row tuple with the data we want
    row = tuple([fields[col - 1] for col in cols])

    # add the current row to our rows list
    rows.append(row)

# insert row list into the database table "genes"
cursor.executemany("INSERT INTO genes VALUES(?, ?, ?, ?, ?)", rows)

# test the insert by dumping the database table genes:
results = cursor.execute("SELECT * FROM genes")
for r in results.fetchall():
    print(r)

这将显示我们想要的列中的数据已插入,产生输出:

(u'one', u'six', u'seven', u'eight', u'ten')
(u'a', u'f', u'g', u'h', u'j')
(u'k', u'p', u'q', u'r', u't')