3

I want to upload a file to s3 without writing the file on my local system, so I am using set_contents_from_string of boto library. I am able to upload this file back to s3.

But how to write an array to a csv file using boto library.

arrlist = [[2, 'jack'], [3, 'john'], [4, 'robert']]
file_name = "test.csv" 
k = Key(bucket, file_name)
k.set_contents_from_string(arrlist)

Its not accepting array, any help how can I achieve this?

1 Answer 1

2

First, turn the array into multi-lines string.

arrlist = [[2, 'jack'], [3, 'john'], [4, 'robert']]

i = 0
multiLine = ""

for item in arrlist:
    newLine = ','.join(map(str, item))

    if (i > 0) {
        multiLine = multiLine + '\n'
    }
    multiLine = multiLine + newLine
    i++

file_name = "test.csv" 
k = Key(bucket, file_name)
k.set_contents_from_string(multiLine)

The output multiLine, which is written to file will be like:

2,jack
3,john
4,robert

Hope this help!

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.