import_csv



In [ ]:
import pandas as pd
import sqlalchemy

LOCAL_DB_URL = "postgres://just_married:just_married@localhost:5440/just_married"

In [ ]:
df = pd.read_csv('/home/andrea/projects/personal/just-married/invitati.csv')

In [ ]:
templ = "INSERT INTO family (invited_by, lunch, family_name, requires_accommodation) VALUES ('{}', {}, '{}', {})"

In [ ]:
dbconn = sqlalchemy.create_engine(LOCAL_DB_URL)

In [ ]:
pd.read_sql("select * from family", dbconn)

In [ ]:
def iter_df(df):
    yield from df.T.to_dict().values()

In [ ]:
def d_to_insert(d):
    name = d['Name']
    lunch = 'true' if d['Lunch'] == 'x' else 'false'
    invited_by = d['Invited by'].lower().strip()
    need_accommodation = 'true' if invited_by == 'enrica' else 'false'
    return templ.format(invited_by, lunch, name, need_accommodation)

In [ ]:
for ins in map(d_to_insert, iter_df(df)):
    dbconn.execute(ins)

In [ ]: