Skip to content

Instantly share code, notes, and snippets.

@krmanik
Created February 27, 2021 15:02
Show Gist options
  • Save krmanik/91dbb8447efda0103b887261325c3079 to your computer and use it in GitHub Desktop.
Save krmanik/91dbb8447efda0103b887261325c3079 to your computer and use it in GitHub Desktop.
Create sqlite db file from TSV file
# change file name, column name and table name
import os
import csv
import sqlite3
def convert():
# enter database file name
conn = sqlite3.connect("chinese_data.db")
curs = conn.cursor()
# enter name of column in tsv files
curs.execute(
"CREATE TABLE examples (id INTEGER PRIMARY KEY, sentence TEXT, english TEXT);")
# enter path to tsv file
tsv_file = "cmn_sentences - Sheet1.tsv"
if os.path.exists(tsv_file):
reader = csv.reader(open(tsv_file, 'r', encoding="utf-8"), delimiter='\t')
for row in reader:
# row to be inserted
to_db = [row[2], row[3]]
curs.execute("INSERT INTO examples (sentence, english) VALUES (?, ?);",
to_db)
conn.commit()
convert()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment