Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def to_sql(engine, df, table, if_exists='fail', sep='t', encoding='utf8',
- schema='public', dtypes_sql=None, verbose=False):
- # Create Table
- ## istruzioni diverse se le colonne hanno dtypes diversi
- if verbose==True:
- print("Scrivo tabella targhe su tabella di schema {}".format(schema))
- if dtypes_sql is None:
- df[:0].to_sql(table, engine, if_exists=if_exists,schema=schema, index=False)
- else:
- df[:0].to_sql(table, engine, if_exists=if_exists,schema=schema, index=False,dtype=dtypes_sql)
- # Prepare data
- output = StringIO()
- df.to_csv(output, sep=sep, header=False, encoding=encoding, index=False)
- output.seek(0)
- # Insert data
- connection = engine.raw_connection()
- cursor = connection.cursor()
- #handling different schemas:
- if schema in ['public','dbo']:
- cursor.copy_from(output, table, sep=sep, null='')
- else:
- new_table = schema + "." + table
- cursor.copy_from(output, new_table, sep=sep, null='')
- connection.commit()
- cursor.close()
- if verbose==True:
- print("Saved")
- return None
- input_file_df.replace(to_replace=b'x00',value=' ', inplace=True,regex=True)
- input_file_df.replace(to_replace="x00", value=" ",inplace=True)
- input_file_df.where(pd.notnull(input_file_df), None,inplace=True)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement