Advertisement
Guest User

Untitled

a guest
Jun 19th, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.30 KB | None | 0 0
  1. def to_sql(engine, df, table, if_exists='fail', sep='t', encoding='utf8',
  2. schema='public', dtypes_sql=None, verbose=False):
  3. # Create Table
  4. ## istruzioni diverse se le colonne hanno dtypes diversi
  5. if verbose==True:
  6. print("Scrivo tabella targhe su tabella di schema {}".format(schema))
  7. if dtypes_sql is None:
  8. df[:0].to_sql(table, engine, if_exists=if_exists,schema=schema, index=False)
  9. else:
  10. df[:0].to_sql(table, engine, if_exists=if_exists,schema=schema, index=False,dtype=dtypes_sql)
  11. # Prepare data
  12. output = StringIO()
  13. df.to_csv(output, sep=sep, header=False, encoding=encoding, index=False)
  14. output.seek(0)
  15.  
  16. # Insert data
  17. connection = engine.raw_connection()
  18. cursor = connection.cursor()
  19. #handling different schemas:
  20. if schema in ['public','dbo']:
  21. cursor.copy_from(output, table, sep=sep, null='')
  22.  
  23. else:
  24. new_table = schema + "." + table
  25. cursor.copy_from(output, new_table, sep=sep, null='')
  26. connection.commit()
  27. cursor.close()
  28. if verbose==True:
  29. print("Saved")
  30. return None
  31.  
  32. input_file_df.replace(to_replace=b'x00',value=' ', inplace=True,regex=True)
  33. input_file_df.replace(to_replace="x00", value=" ",inplace=True)
  34. input_file_df.where(pd.notnull(input_file_df), None,inplace=True)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement