feat: exception checking
This commit is contained in:
parent
761895fb6a
commit
86282b1ab7
@ -1,23 +1,20 @@
|
|||||||
#!/bin/python3
|
#!/bin/python3
|
||||||
import pandas as pd
|
import pandas as pd
|
||||||
import numpy as np
|
|
||||||
from argparse import ArgumentParser
|
from argparse import ArgumentParser
|
||||||
|
|
||||||
|
# Wraps strings and timestamps into single quotes
|
||||||
def wrap_types(val):
|
def wrap_types(val):
|
||||||
if np.isnan(val):
|
if type(val) in [str, pd._libs.tslibs.timestamps.Timestamp] and val != "NULL":
|
||||||
print("NaN FOUND")
|
|
||||||
val = "NULL"
|
|
||||||
if type(val) in [str, pd._libs.tslibs.timestamps.Timestamp]:
|
|
||||||
return f"'{val}'"
|
return f"'{val}'"
|
||||||
else:
|
else:
|
||||||
return val
|
return val
|
||||||
|
|
||||||
p = ArgumentParser()
|
p = ArgumentParser()
|
||||||
# action store_true is for flags
|
|
||||||
p.add_argument("-i", "--input", nargs="+", help="Path to input excel file")
|
p.add_argument("-i", "--input", nargs="+", help="Path to input excel file")
|
||||||
p.add_argument("-o", "--output", nargs="+", help="Path to output sql file")
|
p.add_argument("-o", "--output", nargs="+", help="Path to output sql file")
|
||||||
args = p.parse_args()
|
args = p.parse_args()
|
||||||
|
|
||||||
|
# Manual fallback if argument is not specified
|
||||||
if not args.input:
|
if not args.input:
|
||||||
in_file_path = input("Please enter path to input excel file: ")
|
in_file_path = input("Please enter path to input excel file: ")
|
||||||
else:
|
else:
|
||||||
@ -28,20 +25,26 @@ if not args.output:
|
|||||||
else:
|
else:
|
||||||
out_file_path = args.output[0]
|
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}')
|
print(f'Input: {in_file_path}\nOutput: {out_file_path}')
|
||||||
|
|
||||||
in_file = pd.ExcelFile(in_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:
|
with open(out_file_path, "w") as of:
|
||||||
for sheet in in_file.sheet_names:
|
for sheet in in_file.sheet_names:
|
||||||
curr_meta = in_file.parse(sheet, nrows=1, header=None)
|
print(f'Processing sheet "{sheet}"...')
|
||||||
curr_sheet = in_file.parse(sheet, header=1)
|
db_table = in_file.parse(sheet, nrows=1, header=None)[0][0]
|
||||||
curr_meta = curr_meta[0][0].split("#")
|
curr_sheet = in_file.parse(sheet, header=1).fillna("NULL")
|
||||||
db = curr_meta[0]
|
count = 0
|
||||||
table_prefix = curr_meta[1]
|
|
||||||
table_suffix = sheet
|
|
||||||
# One line is one test subject/object
|
# One line is one test subject/object
|
||||||
for line in curr_sheet.iterrows():
|
for line in curr_sheet.iterrows():
|
||||||
indices = line[1].index
|
indices = line[1].index
|
||||||
@ -49,7 +52,6 @@ with open(out_file_path, "w") as of:
|
|||||||
cols = ""
|
cols = ""
|
||||||
vals = ""
|
vals = ""
|
||||||
for i in range(len(indices)):
|
for i in range(len(indices)):
|
||||||
# print(type(values[i]), ": ", values[i])
|
|
||||||
if i != 0:
|
if i != 0:
|
||||||
cols += f", {indices[i]}"
|
cols += f", {indices[i]}"
|
||||||
vals += f", {wrap_types(values[i])}"
|
vals += f", {wrap_types(values[i])}"
|
||||||
@ -57,4 +59,6 @@ with open(out_file_path, "w") as of:
|
|||||||
cols += f"{indices[i]}"
|
cols += f"{indices[i]}"
|
||||||
vals += f"{wrap_types(values[i])}"
|
vals += f"{wrap_types(values[i])}"
|
||||||
# INSERT INTO $DB.$TABLE ($COL1, $COL2, ...) VALUES ($VAL1, $VAL2, ...)
|
# 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')
|
||||||
|
Loading…
x
Reference in New Issue
Block a user