Forked from jitsejan/_write_dataframe_to_csv_on_s3.py
Created
December 27, 2018 06:06
-
-
Save lxneng/e95533a3ee2dba305afd95c918fd1500 to your computer and use it in GitHub Desktop.
Write a Pandas dataframe to CSV format on AWS S3.
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
import boto3 | |
from io import StringIO | |
def _write_dataframe_to_csv_on_s3(dataframe, filename): | |
""" Write a dataframe to a CSV on S3 """ | |
print("Writing {} records to {}".format(len(dataframe), filename)) | |
# Create buffer | |
csv_buffer = StringIO() | |
# Write dataframe to buffer | |
dataframe.to_csv(csv_buffer, sep="|", index=False) | |
# Create S3 object | |
s3_resource = boto3.resource("s3") | |
# Write buffer to S3 object | |
s3_resource.Object(DESTINATION, filename).put(Body=csv_buffer.getvalue()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment