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.
49 lines
1.5 KiB
49 lines
1.5 KiB
#!/bin/python3
|
|
import pandas as pd
|
|
from argparse import ArgumentParser
|
|
|
|
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)
|
|
|
|
with open(out_file_path, "w") as of:
|
|
for sheet in in_file.sheet_names:
|
|
curr_sheet = in_file.parse(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)):
|
|
if i != 0:
|
|
cols += f", {indices[i]}"
|
|
vals += f", {values[i]}"
|
|
else:
|
|
cols += f"{indices[i]}"
|
|
vals += f"{values[i]}"
|
|
# USE $DB
|
|
# 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")
|