본문 바로가기

Language/python

[ Python library ] get_balancesheet함수를 통해 최근 5개 분기 재무상태표를 불러와서 linkedjemun테이블에 insert하기

반응형

#  get_balancesheet함수를 통해 최근 5개 분기 재무상태표를 불러와서 linkedjemun테이블에 insert하기
def insertlinkedjemun(shcode):
    df_raw = get_balancesheet(shcode) # get_balancesheet함수를 통해 최근 5개 분기 재무상태표를 불러옴.
    insertsql=""
    pd.set_option('display.max_colwidth', -1)
    pd.set_option('display.max_columns', 15)

    con = psycopg2.connect("dbname='webcroll' user='postgres' host='localhost' password='1111'")
    cur = con.cursor()

    if df_raw.empty != True:
        for j in range(len(df_raw)):
            colstring = ""
            for k in range(250):
                colvalue = df_raw.iloc[j][k]
                if colvalue is None:
                    colvalue = 0
                colvalue = round(colvalue,1)
                colstring = colstring + ',' + str(colvalue)
            insertsql = "insert into linkedjemun values(nextval('jemu_id_seq')," + '\'' + shcode + '\'' + "," + '\''+str(df_raw.index[j])[:10]+'\''+ colstring + ");"
            deletesql = "delete from linkedjemun where code=" + '\'' + shcode + '\'' + " and " + "indexdate ="+ '\''+str(df_raw.index[j])[:10]+'\''+  ";"
            print(deletesql)
            print(insertsql)
            cur.execute(deletesql)
            cur.execute(insertsql)
            cur.execute("commit;")
    cur.close()
    con.close()
    return

shcode = '005930'
insertlinkedjemun(shcode)

반응형