I've got a directory of csv files which I would rather have as a sqlite3 database. What is the best way to write each csv file as a table in a database?
1 Answer
I found a solution
import pandas as pd #IO
import sqlite3 #To write database
import glob #loop through working directory
cxn = sqlite3.connect('mydb.sqlite3')
for file in glob.glob('*.csv'):
table_name = file[:-4] #File name without .csv
df = pd.read_csv(file)
df.to_sql(table_name, cxn, index = False)
1 Comment
Bill Bell
In case others come here to find a solution, would you consider editing your answer to show your import of pandas?