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?

1 Answer 1

6

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)
Sign up to request clarification or add additional context in comments.

1 Comment

In case others come here to find a solution, would you consider editing your answer to show your import of pandas?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.