1

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?

JohnE
  • 29,156
  • 8
  • 79
  • 109
Demetri Pananos
  • 6,770
  • 9
  • 42
  • 73

1 Answers1

5

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)
Demetri Pananos
  • 6,770
  • 9
  • 42
  • 73
  • In case others come here to find a solution, would you consider editing your answer to show your import of pandas? – Bill Bell Dec 04 '16 at 16:07