Quantcast
Channel: Active questions tagged excel - Stack Overflow
Viewing all articles
Browse latest Browse all 88030

pandas add sql table, check if table exists

$
0
0

I have this piece of code that loops for excel files in a directory, add the file in a sqlite db . I managed to get past the exception raised if the table exists but I find this inelegant and inefficient since the loop reads the excel file, add it in a Dataframe, etc... what ideally i would like is that I test the existence of the table before creating the df from excel.

response = {}
for f in glob('T:\GESTION\toto\titi\tata\file_201*.xlsx'):
    print f
    datereg = re.search('T:\\\\GESTION\\\\toto\\\\titi\\\\tata\\\\file_(\d{4})(\d{2})(\d{2}).xlsx', f)
    if datereg is not None:
        dated = datetime.datetime(int(datereg.group(1)), int(datereg.group(2)), int(datereg.group(3)))
    print dated

    # ideally test if table in db exists here

    xl = pd.ExcelFile(f)
    df = xl.parse(sheetname="Sheet1")
    df = df[extractFields].drop_duplicates(subset='ISIN')
    df = df.set_index('ISIN', verify_integrity=True)
    response[dated] = df
    # print response
    engine = sqlalchemy.create_engine('sqlite:///my_db.sqlite')
    try:
        df.to_sql(dated.__str__(), engine, if_exists='fail')
    except ValueError as err:
        print(err)
        pass

Viewing all articles
Browse latest Browse all 88030

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>