数据库
首页 > 数据库> > 如何使用Python API将文本文件放入sqlite?

如何使用Python API将文本文件放入sqlite?

作者:互联网

我有一个以制表符分隔的文件格式:

sentenceID (sid)    documentID (scid)   sentenceText (sent)

例如.

100004  100 即便您喜爱流连酒吧,也定然在这轻松安闲的一隅,来一场甜蜜沉醉的约会.
100005  100 您可以慢慢探究菜单上所有的秘密惊喜.

我想用以下架构将它放入sqlite3:

CREATE TABLE sent (
    sid INTEGER PRIMARY KEY,
    scid INTEGER,
    sent TEXT,
    );

有没有一种快速的方法来使用python API for sqlite(http://docs.python.org/2/library/sqlite3.html)将它们放入表中?

我一直在这样做:

#!/usr/bin/python
# -*- coding: utf-8 -*-

import sqlite3 as lite
import sys, codecs

con = lite.connect('mycorpus.db')

with con:    
    cur = con.cursor()
    cur.execute("CREATE TABLE Corpus(sid INT, scid INT, sent TEXT, PRIMARY KEY (sid))")
    for line in codecs.read('corpus.tab','r','utf8'):
        sid,scid,sent = line.strip().split("\t")
        cur.execute("INSERT INTO Corpus VALUES("+sid+","+scid+"'"+sent+"')")

解决方法:

以下是使用unicodecsv模块的示例:

#!/usr/bin/python
# -*- coding: utf-8 -*-

import sqlite3

import unicodecsv


con = sqlite3.connect('mycorpus.db')
cur = con.cursor()
cur.execute("CREATE TABLE Corpus(sid INT, scid INT, sent TEXT, PRIMARY KEY (sid))")

with open('corpus.tab', 'rb') as input_file:
    reader = unicodecsv.reader(input_file, delimiter="\t")
    data = [row for row in reader]

cur.executemany("INSERT INTO Corpus (sid, scid, sent) VALUES (?, ?, ?);", data)
con.commit()

另见:

> Reading a UTF8 CSV file with Python
> Importing a CSV file into a sqlite3 database table using Python

希望有所帮助.

标签:python,database,sqlite3,api,tab-delimited
来源: https://codeday.me/bug/20190612/1229151.html