Created
April 4, 2024 19:42
-
-
Save Arqentum/12ccdb40c6b5560e8b6c22abfad3c427 to your computer and use it in GitHub Desktop.
#Pandas to #GCS gzipped
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| csv_file_name = get_file_name(execution_date=context['execution_date'], ripley_table_name=self.ripley_table_name) | |
| LOGGER.info('Uploading %s to %s' % (csv_file_name, self.gcs_bucket_name)) | |
| # Ensure that we properly encode and escape the JSON string | |
| pandas_df['payload'] = pandas_df['payload'].apply(json.dumps) | |
| pandas_df['schema'] = pandas_df['schema'].apply(json.dumps) | |
| correct_csv = pandas_df.to_csv(encoding='utf-8', header=False, index=False, doublequote=True, quoting=csv.QUOTE_ALL) | |
| blob = bucket.blob(csv_file_name) | |
| blob.content_encoding = "gzip" | |
| blob.content_type = "text/csv" | |
| writer = fileio.BlobWriter(blob) | |
| gz = gzip.GzipFile(filename=csv_file_name, fileobj=writer, mode="w") | |
| gz.write(bytes(correct_csv, encoding='utf-8')) | |
| gz.close() | |
| writer.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment