Skip to content

Instantly share code, notes, and snippets.

@andres-alvarado

andres-alvarado/script.py

Last active Nov 16, 2020
Embed
What would you like to do?
import pandas as pd
import glob
from sqlalchemy import create_engine as ce, types
def insertToDatabase( p_df, p_tbl ):
# Idealmente, poner en un archivo .const.
# Se pone aqui para mas facilidad y hacer menos lineas.
con = "mysql://usuario:contrasenia@localhost/nombreDeBaseDeDatos?use_unicode=1&charset=utf8"
p_df.to_sql( name = p_tbl
, con = ce( con )
, if_exists = "append"
, index = False
)
def processCsvs( dir, p_tbl ):
# Recorrer todo el directorio en busca de CSV's.
# glob permite encontrar patrones en el nombre de archivos.
for csv in glob.glob(f"{dir}/*.csv"):
print(f"Processing {csv}")
df = pd.read_csv( filepath_or_buffer = csv, header = 0 )
insertToDatabase( p_df = df, p_tbl = p_tbl )
# Main
games_dir = # Directorio que contiene los csv's de partidos
events_dir = # Directorio que contiene los csv's de eventos.
processCsvs( dir = games_dir, p_tbl = "s_retrosheet_games" )
processCsvs( dir = events_dir, p_tbl = "s_retrosheet_events" )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.