You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

61 lines
2.0 KiB

#!/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")
p.add_argument("-o", "--output", nargs="+", help="Path to output sql file")
args = p.parse_args()
if not args.input:
in_file_path = input("Please enter path to input excel file: ")
else:
in_file_path = args.input[0]
if not args.output:
out_file_path = input("Please enter path to output sql file: ")
else:
out_file_path = args.output[0]
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
# One line is one test subject/object
for line in curr_sheet.iterrows():
indices = line[1].index
values = line[1].values
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])}"
else:
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')