diff --git a/melli/excel2sql.py b/melli/excel2sql.py index 4aad133..a325b7b 100755 --- a/melli/excel2sql.py +++ b/melli/excel2sql.py @@ -1,7 +1,17 @@ #!/bin/python3 import pandas as pd +import numpy as np from argparse import ArgumentParser +def wrap_types(val): + if np.isnan(val): + print("NaN FOUND") + val = "NULL" + if type(val) in [str, pd._libs.tslibs.timestamps.Timestamp]: + 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") @@ -22,27 +32,29 @@ else: 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_sheet = in_file.parse(sheet) + 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 # One line is one test subject/object for line in curr_sheet.iterrows(): indices = line[1].index values = line[1].values - # print(indices, values) cols = "" vals = "" for i in range(len(indices)): + # print(type(values[i]), ": ", values[i]) if i != 0: cols += f", {indices[i]}" - vals += f", {values[i]}" + vals += f", {wrap_types(values[i])}" else: cols += f"{indices[i]}" - vals += f"{values[i]}" - # USE $DB + vals += f"{wrap_types(values[i])}" # INSERT INTO $DB.$TABLE ($COL1, $COL2, ...) VALUES ($VAL1, $VAL2, ...) - db_table = sheet.split(".") - db = db_table[0] - table = db_table[1] - of.write(f"INSERT INTO {db}.{table} ({cols}) VALUES ({vals})\n") + of.write(f'INSERT INTO "{db}"."{table_prefix}.{table_suffix}" ({cols}) VALUES ({vals})\n')