Created
May 18, 2017 16:43
-
-
Save brockthebear/7ed968dd2bee3a7ea916e4be1922003f to your computer and use it in GitHub Desktop.
Import CSV file to MySQL database
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
""" | |
Essentially just a python script to run the sql command below. | |
mysqlimport --ignore-lines=1 --fields-terminated-by=, --columns=csv_headers --local -u root -p db_name file.csv | |
""" | |
import argparse | |
import subprocess as sp | |
import sys | |
import os | |
from os import path | |
import csv | |
def _parse_args(args=None): | |
p = argparse.ArgumentParser(description="Import a CSV file into SQL") | |
p.add_argument('-i', '--input', help="The CSV to import. File basename must match table name.", required=True) | |
p.add_argument('-d', '--delimiter', help="The delimiter character.", default=",") | |
p.add_argument('-db', '--database', help="The database to insert the data into.", required=True) | |
return p.parse_args(args) | |
def _get_file_headers(file): | |
with open(file, 'r') as f: | |
reader = csv.DictReader(f) | |
i = reader.fieldnames | |
return i | |
def main(): | |
args = _parse_args() | |
headers = _get_file_headers(args.input) | |
filepath = path.abspath(args.input) | |
delim = args.delimiter | |
db = args.database | |
cmd = [ | |
'mysqlimport', | |
'--ignore-lines=1', | |
'--fields-terminated-by={}'.format(delim), | |
'--columns={}'.format(','.join(headers)), | |
'--local', | |
'-u', | |
'root', | |
'-p', | |
args.database, | |
filepath | |
] | |
return sp.call(cmd, stdout=sys.stdout, stderr=sp.STDOUT) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment