database - How to put a textfile into sqlite simply using Python API? -


i have tab delimited file in format:

sentenceid (sid)    documentid (scid)   sentencetext (sent) 

e.g.

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

i want put sqlite3 following schema:

create table sent (     sid integer primary key,     scid integer,     sent text,     ); 

is there quick way use python api sqlite (http://docs.python.org/2/library/sqlite3.html) put them table?

i've been doing such:

#!/usr/bin/python # -*- coding: utf-8 -*-  import sqlite3 lite import sys, codecs  con = lite.connect('mycorpus.db')  con:         cur = con.cursor()     cur.execute("create table corpus(sid int, scid int, sent text, primary key (sid))")     line in codecs.read('corpus.tab','r','utf8'):         sid,scid,sent = line.strip().split("\t")         cur.execute("insert corpus values("+sid+","+scid+"'"+sent+"')") 

here's example using unicodecsv module:

#!/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))")  open('corpus.tab', 'rb') input_file:     reader = unicodecsv.reader(input_file, delimiter="\t")     data = [row row in reader]  cur.executemany("insert corpus (sid, scid, sent) values (?, ?, ?);", data) con.commit() 

also see:

hope helps.


Comments

Popular posts from this blog

Change php variable from jquery value using ajax (same page) -

Pull out data related to my apps from Android Play Store and iOS App Store -

How can I fetch data from a web server in an android application? -