Pandas DF列到SQLite表

时间:2017-07-15 16:24:36

标签: python sqlite csv

我有一个我在sqlite中创建的数据库,它是根据从csv文件创建的pandas数据帧字典创建的。当我将数据库导入sqlite时,每个表名都以" .csv"结尾。我该怎么剥掉这个?

以下是代码:

import os
import glob
import pandas as pd

files = glob.glob(os.path.join("staging" + "/*.csv"))

print(files)

# Create an empty dictionary to hold the dataframes from csvs
dict_ = {}

# Write the files into the dictionary
for file in files:
    fname = os.path.basename(file)
    dict_[fname] = pd.read_csv(file, header = 0, dtype = str, encoding = 'cp1252').fillna('')

对于sqlite DB:

# Create the SQL Lite database
import sqlite3

conn = sqlite3.connect("medicare_hospital_compare.db")

# Convert the dict_[file]'s to SQL tables
for key, df in dict_.items(): 
    df.to_sql(key, conn, flavor = None, schema = None, if_exists = 'replace', 
              index = True, index_label = None, chunksize = None, dtype = None)

0 个答案:

没有答案