I currently use the following code to create a binary file that I then directly upload into AWS S3. Now I was told it's possible to write with the csv.writer directly into the binary mode and avoid the extra step with io.StringIO(). How does that work?
buffer = io.StringIO()
writer = csv.writer(buffer)
writer.writerow(["a", "b", "c"])
buffer_2 = io.BytesIO(buffer.getvalue().encode())
BUCKET_NAME = 'fbprophet'
OBJECT_NAME = 'blah.csv'
s3.upload_fileobj(buffer_2, BUCKET_NAME, OBJECT_NAME)
Python 3openisn't going to cut it. I had an answer already written, the short of it is: your approach is fine, I don't see a way to significantly improve it. Just be sure to passencoding('utf-8')because Python's default encoding is system-depedent.