Last active
February 13, 2024 12:53
-
-
Save jitsejan/e24c6f9b288a839f40edd1ce944a747e to your computer and use it in GitHub Desktop.
Write a Pandas dataframe to CSV format on AWS S3.
This file contains 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
I assume you can use the
encoding
parameter of Pandasto_csv
. Are you able to addencoding="utf-8"
to thedataframe.to_csv()
step?