使用python从多个csv文件构建sqlite数据库 [英] Using python to build an sqlite database from multiple csv files

查看:496
本文介绍了使用python从多个csv文件构建sqlite数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用Python(Enthought Canopy Editor)从csv文件构建一个sqlite数据库。
我想将一个文件夹中包含的所有csv文件导入单独的表,表名根据csv文件名分配。
到目前为止,我已经设法复制和粘贴在网上找到的代码块如下:

I am trying to use Python (Enthought Canopy Editor) to build an sqlite database from csv files. I would like to import all csv files contained in a folder into separate tables, with the table name being assigned based on the csv filename. So far I have managed to copy and paste chunks of code found online into the following:

import csv
import sqlite3
# Create the database
connection = sqlite3.connect('C:/ROAST/3_ANALYSIS/03_SQL-PY/primo.db')
cursor = connection.cursor()

# Create the table
cursor.execute('DROP TABLE IF EXISTS A08')
cursor.execute('CREATE TABLE  A08 (uno, due, tre) ')
connection.commit()

# Load the CSV file into CSV reader
csvfile = open('C:/ROAST/3_ANALYSIS/03_SQL-PY\A08_csv/A08_B1_T5.csv', 'rb')
creader = csv.reader(csvfile, delimiter=',', quotechar='|')

# Iterate through the CSV reader, inserting values into the database
for t in creader:
cursor.execute('INSERT INTO A08 VALUES (?,?,?)', t )

# Close the csv file, commit changes, and close the connection
csvfile.close()
connection.commit()
connection.close()'


推荐答案

这是我如何处理您的问题。

Here is how I might approach your problem.

import csv
import sqlite3
import glob
import os

def do_directory(dirname, db):
    for filename in glob.glob(os.path.join(dirname, '*.csv')):
        do_file(filename, db)

def do_file(filename, db):
        with open(filename) as f:
            with db:
                data = csv.DictReader(f)
                cols = data.fieldnames
                table=os.path.splitext(os.path.basename(filename))[0]

                sql = 'drop table if exists "{}"'.format(table)
                db.execute(sql)

                sql = 'create table "{table}" ( {cols} )'.format(
                    table=table,
                    cols=','.join('"{}"'.format(col) for col in cols))
                db.execute(sql)

                sql = 'insert into "{table}" values ( {vals} )'.format(
                    table=table,
                    vals=','.join('?' for col in cols))
                db.executemany(sql, (list(map(row.get, cols)) for row in data))

if __name__ == '__main__':
    conn = sqlite3.connect('foo.db')
    do_directory('.', conn)

根据需要替换最后的 __ main __ 节。也许你会想:

Replace the final __main__ section as appropriate. Perhaps you'll want:

connection = sqlite3.connect('C:/ROAST/3_ANALYSIS/03_SQL-PY/primo.db')
do_file('C:/ROAST/3_ANALYSIS/03_SQL-PY\A08_csv/A08_B1_T5.csv', connection)

connection = sqlite3.connect('C:/ROAST/3_ANALYSIS/03_SQL-PY/primo.db')
do_directory('C:/ROAST/3_ANALYSIS/03_SQL-PY\A08_csv',connection)

这篇关于使用python从多个csv文件构建sqlite数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆