1

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)
6
  • python version 2 or 3? Commented Aug 16, 2019 at 7:06
  • It's Python 3 Commented Aug 16, 2019 at 7:07
  • refer stackoverflow.com/questions/5358322/… Commented Aug 16, 2019 at 7:09
  • I mentioned that link in my post, but that's where I got stuck, I don't understand how to modify my code to manage the same. Commented Aug 16, 2019 at 7:12
  • 3
    I don't believe this is a duplicate. @JoeyCoder is not writing a file, but rather an in-memory file-like object, so passing flags to open isn'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 pass encoding('utf-8') because Python's default encoding is system-depedent. Commented Aug 16, 2019 at 7:13

1 Answer 1

3

What you've got there looks reasonable to me. The post you link to talks about writing to files, not in-memory streams. A file can be opened in either text or binary mode, which determines whether it operates on strings (str) or raw bytes (bytes). But the in-memory file-like objects from the io package aren't as flexible: you have StringIO for strings, and BytesIO for bytes.

Because csv requires a text stream (strings), and boto requires a binary stream (bytes), a conversion step is necessary.

I would recommend to pass the actual encoding to the encode() function though, to avoid falling back to Python's system-dependent default:

buffer_2 = io.BytesIO(buff.getvalue().encode('utf-8'))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.