Generate an insert statement from CSV with Python.

Generate an insert statement from CSV with Python.

Overview

A script that prepares CSV according to certain rules and generates insert statements from it You can see examples of making similar things with Excel sheets.

What are certain rules?

  1. The file name (excluding the extension) is the table name
  2. It is assumed that the csv has a header and the column name matches the DB column name.

There is a problem, and CSV alone does not have type information for the table, so it is not possible to determine whether or not it will come with''. In this example, it is all enclosed once. In practice, the idea is to replace it with a regular expression later.

Usage environment

Python 3.7.2 Visual Studio Code 1.39.1

Source code

CSV2insSQL.py



#Generate an insert statement from CSV.
#rule
#file name(Excluding the extension)Is the table name
#It is assumed that the csv has a header and the column name matches the DB column name.
#Task!!CSV alone does not have type information for the table, so''I can't judge whether it's big or not
#In this example, it is all enclosed once(Practically, later replace with a regular expression)


import csv
import os

FROM_CSV = '.\\CSV\\' + "xxx_LIST.csv"

def main():
    #Body processing
    #...Master insert generation
    with open(".\\CSV\\insertSQL.txt", "w", newline="",encoding="cp932") as wf:
        #From file name to table name
        tablename = os.path.splitext(os.path.basename(FROM_CSV))[0]
        
        with open(FROM_CSV, encoding="cp932",) as f:
            reader = csv.DictReader(f)

            for row in reader:
                #insert statement generation
                insert_sql = "insert into " + tablename
                fields = ",".join(reader.fieldnames)
                insert_sql =  insert_sql + "( " + fields + ") VALUES"
                valuelist = []
                for fld in reader.fieldnames:
                   #Task!!CSV alone does not have type information for the table, so''I can't judge whether it's big or not
                   #In this example, it is all enclosed once
                   valuelist.append("'" + row[fld] + "'")

                valustr = ",".join(valuelist)
                insert_sql =  insert_sql + "( " + valustr + ");"

                wf.write(insert_sql+"\n")


if __name__ == '__main__': main()

Recommended Posts

Generate an insert statement from CSV with Python.
Scraping from an authenticated site with python
Csv output from Google search with [Python]! 【Easy】
Csv tinkering with python
Make JSON into CSV with Python from Splunk
Remove headings from multiple format CSV files with python
Read csv with python pandas
Generate XML (RSS) with Python
Write to csv with Python
With skype, notify with skype from python!
Download csv file with python
I tried sending an email from Amazon SES with Python
Notes on importing data from MySQL or CSV with Python
Turn an array of strings with a for statement (Python3)
Try to generate a cyclic peptide from an amino acid sequence with Python and RDKit
Precautions when inputting from CSV with Python and outputting to json to make it an exe
[Python] What is a with statement?
Cut out an image with python
Call C from Python with DragonFFI
Make apache log csv with python
Install Python from source with Ansible
Operate an I2C-connected display from Python
Output to csv file with Python
Create an Excel file with Python3
I sent an SMS with Python
Handle Excel CSV files with Python
[Python] Generate a password with Slackbot
Call python from nim with Nimpy
Reading and writing CSV with Python
Draw an illustration with Python + OpenCV
Read fbx from python with cinema4d
[Python] Send an email with outlook
Try to extract a character string from an image with Python3
Extract database tables with CSV [ODBC connection from R and python]
Python hand play (CSV is applied with Pandas DataFrame, but only full-column Insert from CSV to DB?)
Collecting information from Twitter with Python (Twitter API)
[Python] Building an environment with Anaconda [Mac]
Receive textual data from mysql with python
Get html from element with Python selenium
[Python] Super easy test with assert statement
INSERT into MySQL with Python [For beginners]
[Note] Get data from PostgreSQL with Python
Play audio files from Python with interrupts
Create wordcloud from your tweet with python3
[Data science basics] I tried saving from csv to mysql with python
Read CSV file with python (Download & parse CSV file)
Generate Japanese test data with Python faker
Convert from PDF to CSV with pdfplumber
Note when creating an environment with python
Quickly create an excel file with Python #python
I tried sending an email with python.
Business efficiency starting from scratch with Python
Working with Azure CosmosDB from Python Part.2
Image acquisition from camera with Python + OpenCV
Getting started with Dynamo from Python boto
When issuing an INSERT statement in Python, no such column is displayed
python + faker Randomly generate a point with a radius of 100m from a certain point
Try calling Python from Ruby with thrift
[Python] Quickly create an API with Flask
Generate a class from a string in Python
[Part1] Scraping with Python → Organize to csv!