Last active
May 20, 2020 18:13
-
-
Save joffilyfe/33aef34f1882d3d7f50b34ca5c88a9ed to your computer and use it in GitHub Desktop.
Converts an CSV file to SQL insert file
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
import csv | |
import argparse | |
def translate_to_sql(source_file, output_file, table, skip_header=False, columns=[]): | |
if skip_header and len(columns) == 0: | |
raise TypeError( | |
"É preciso informar as colunas quando o cabeçalho do CSV é evitado" | |
) | |
def format_value_to_sql(value: str): | |
if value.strip().isdigit(): | |
return value.strip() | |
return "'%s'" % value | |
if skip_header: | |
reader = csv.DictReader(source_file, fieldnames=columns) | |
else: | |
reader = csv.DictReader(source_file) | |
_ = next(reader) | |
columns = reader.fieldnames | |
for row in reader: | |
values = [format_value_to_sql(row[column]) for column in columns] | |
query = "INSERT INTO {table}({columns}) VALUES ({values});" | |
string_columns = ", ".join(['"%s"' % column for column in columns]) | |
string_values = ", ".join(values) | |
query = query.format(table=table, columns=string_columns, values=string_values) | |
output_file.write(query + "\n") | |
def main(): | |
parser = argparse.ArgumentParser( | |
description="Transforma um arquivo CSV em inserções em uma base SQL" | |
) | |
parser.add_argument( | |
"file", | |
type=argparse.FileType("r"), | |
help="Arquivo CSV contendo informações do motor de extração", | |
) | |
parser.add_argument( | |
"output", | |
type=argparse.FileType("w"), | |
help="Define um arquivo de saída para o conteúdo transformado", | |
) | |
parser.add_argument("table", help="Nome da tabela utilizada para formatação do SQL") | |
parser.add_argument("--skip-header", action="store_true") | |
parser.add_argument( | |
"--columns", | |
default=[], | |
nargs="+", | |
help="Define as colunas que serão utilizadas para formar a inserção SQL", | |
) | |
args = parser.parse_args() | |
translate_to_sql( | |
source_file=args.file, | |
output_file=args.output, | |
table=args.table, | |
skip_header=args.skip_header, | |
columns=args.columns, | |
) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment