diff options
Diffstat (limited to 'msgPack2sqlite_msgPack.py')
-rw-r--r-- | msgPack2sqlite_msgPack.py | 73 |
1 files changed, 45 insertions, 28 deletions
diff --git a/msgPack2sqlite_msgPack.py b/msgPack2sqlite_msgPack.py index b77dd2e..c251d59 100644 --- a/msgPack2sqlite_msgPack.py +++ b/msgPack2sqlite_msgPack.py @@ -2,36 +2,53 @@ import msgpack import ui +import sys import sqlite3 - -with open('result_all.pack', 'rb') as f: - r = f.read() - -d = p = msgpack.unpackb(r, raw=False) -del r - -with sqlite3.connect("result_all.sql") as con: - cur = con.cursor() - cur.execute('''CREATE TABLE IF NOT EXISTS entry ( - word TEXT, - cat_gram TEXT, - API TEXT, - infos TEXT, - genre TEXT, - accord TEXT, - defs BLOG, - ID INTEGER PRIMARY KEY)''') - con.commit() - - for w, listW in d.items(): - for word in listW: - data = (w, word['cat-gram'], word['API'], "\t".join(word['infos']), - word['genre'], word['accord'], - msgpack.packb(word['def'])) - cur.execute('''INSERT INTO entry (word, cat_gram, API, infos, - genre, accord, defs) VALUES (?, ?, ?, ?, ?, ?, ?)''', data) - con.commit() +import argparse + +if __name__ == '__main__': + parser = argparse.ArgumentParser(description='wiktionary dump msgpack ' + 'to SQLite database file') + parser.add_argument('-o', '--out', dest='outputF', action='store', + help='the output filename') + parser.add_argument('-i', '--input', dest='inputF', action='store', + help='the input filename, a dump of witionary') + + arg = parser.parse_args() + + if arg.inputF is None: + print('Error input file needed', file=sys.stderr) + if arg.outputF is None: + print('Error output file needed', file=sys.stderr) + + with open(arg.inputF, 'rb') as f: + r = f.read() + + d = p = msgpack.unpackb(r, raw=False) + del r + + with sqlite3.connect(arg.outputF) as con: + cur = con.cursor() + cur.execute('''CREATE TABLE IF NOT EXISTS entry ( + word TEXT, + cat_gram TEXT, + API TEXT, + infos TEXT, + genre TEXT, + accord TEXT, + defs BLOG, + ID INTEGER PRIMARY KEY)''') + con.commit() + + for w, listW in d.items(): + for word in listW: + data = (w, word['cat-gram'], word['API'], "\t".join(word['infos']), + word['genre'], word['accord'], + msgpack.packb(word['def'])) + cur.execute('''INSERT INTO entry (word, cat_gram, API, infos, + genre, accord, defs) VALUES (?, ?, ?, ?, ?, ?, ?)''', data) + con.commit() def give_def(w): |