Created
September 14, 2021 02:38
-
-
Save lxndr-rl/d01cd17784409ce4ddde1fbb8b2535e8 to your computer and use it in GitHub Desktop.
Excel table to Sql insert
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pandas as pd | |
import sys | |
if(len(sys.argv[1:]) < 2): | |
print("Uso: py script.py _NOMBRETABLA_ --uppercase|--lowercase(opcional)") | |
exit() | |
try: | |
argumento = sys.argv[3] | |
except: | |
argumento = "invalid" | |
df = pd.read_excel(sys.argv[2], sheet_name='Sheet1') | |
if(argumento != '--uppercase' and argumento != '--lowercase'): | |
print("Opcion invalida. Se usará lowercase\n") | |
argumento = '--lowercase' | |
stringini = f"insert into {sys.argv[1]} VALUES(" | |
stringValues = "" | |
first = True | |
for i in df.values: | |
for j in i: | |
if first: | |
if(argumento == '--uppercase'): | |
stringValues = f"{stringini.upper()}{j}" | |
else: | |
stringValues = f"{stringini.lower()}{j}" | |
first = False | |
else: | |
if(type(j) == int or type(j) == float): | |
stringValues = f"{stringValues}, {j}" | |
else: | |
if(argumento == '--uppercase'): | |
stringValues = f"{stringValues}, '{j.upper()}'" | |
else: | |
stringValues = f"{stringValues}, '{j}'" | |
print(f"{stringValues})") | |
first = True | |
stringValues = '' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Convert
To