From 86282b1ab7f2d5befb550c0ee7953aa930b547d0 Mon Sep 17 00:00:00 2001 From: phga Date: Wed, 20 Oct 2021 18:11:14 +0200 Subject: [PATCH] feat: exception checking --- melli/excel2sql.py | 34 +++++++++++++++++++--------------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/melli/excel2sql.py b/melli/excel2sql.py index a325b7b..b170497 100755 --- a/melli/excel2sql.py +++ b/melli/excel2sql.py @@ -1,23 +1,20 @@ #!/bin/python3 import pandas as pd -import numpy as np from argparse import ArgumentParser +# Wraps strings and timestamps into single quotes def wrap_types(val): - if np.isnan(val): - print("NaN FOUND") - val = "NULL" - if type(val) in [str, pd._libs.tslibs.timestamps.Timestamp]: + if type(val) in [str, pd._libs.tslibs.timestamps.Timestamp] and val != "NULL": return f"'{val}'" else: return val p = ArgumentParser() -# action store_true is for flags p.add_argument("-i", "--input", nargs="+", help="Path to input excel file") p.add_argument("-o", "--output", nargs="+", help="Path to output sql file") args = p.parse_args() +# Manual fallback if argument is not specified if not args.input: in_file_path = input("Please enter path to input excel file: ") else: @@ -28,20 +25,26 @@ if not args.output: else: out_file_path = args.output[0] +# Simple input argument checks +if out_file_path.endswith(("xlsx", "xls")): + print("Aborting task. Possible overwrite of source file detected.") + print(f"Specified output file '{out_file_path}' ends with .xls/.xlsx") + exit(1) + +if not in_file_path.endswith(("xlsx", "xls")): + print("Aborting task. No input file in excel format specified.") + exit(1) print(f'Input: {in_file_path}\nOutput: {out_file_path}') in_file = pd.ExcelFile(in_file_path) -print(in_file.parse("Datei", skiprows=1).head()) with open(out_file_path, "w") as of: for sheet in in_file.sheet_names: - curr_meta = in_file.parse(sheet, nrows=1, header=None) - curr_sheet = in_file.parse(sheet, header=1) - curr_meta = curr_meta[0][0].split("#") - db = curr_meta[0] - table_prefix = curr_meta[1] - table_suffix = sheet + print(f'Processing sheet "{sheet}"...') + db_table = in_file.parse(sheet, nrows=1, header=None)[0][0] + curr_sheet = in_file.parse(sheet, header=1).fillna("NULL") + count = 0 # One line is one test subject/object for line in curr_sheet.iterrows(): indices = line[1].index @@ -49,7 +52,6 @@ with open(out_file_path, "w") as of: cols = "" vals = "" for i in range(len(indices)): - # print(type(values[i]), ": ", values[i]) if i != 0: cols += f", {indices[i]}" vals += f", {wrap_types(values[i])}" @@ -57,4 +59,6 @@ with open(out_file_path, "w") as of: cols += f"{indices[i]}" vals += f"{wrap_types(values[i])}" # INSERT INTO $DB.$TABLE ($COL1, $COL2, ...) VALUES ($VAL1, $VAL2, ...) - of.write(f'INSERT INTO "{db}"."{table_prefix}.{table_suffix}" ({cols}) VALUES ({vals})\n') + of.write(f'INSERT INTO {db_table} ({cols}) VALUES ({vals})\n') + count += 1 + print(f'Created {count} INSERT statements for sheet "{sheet}"\n')